src/share/classes/java/lang/Short.java

Print this page




  63 
  64     /**
  65      * Returns a new {@code String} object representing the
  66      * specified {@code short}. The radix is assumed to be 10.
  67      *
  68      * @param s the {@code short} to be converted
  69      * @return the string representation of the specified {@code short}
  70      * @see java.lang.Integer#toString(int)
  71      */
  72     public static String toString(short s) {
  73         return Integer.toString((int)s, 10);
  74     }
  75 
  76     /**
  77      * Parses the string argument as a signed {@code short} in the
  78      * radix specified by the second argument. The characters in the
  79      * string must all be digits, of the specified radix (as
  80      * determined by whether {@link java.lang.Character#digit(char,
  81      * int)} returns a nonnegative value) except that the first
  82      * character may be an ASCII minus sign {@code '-'}
  83      * (<code>'&#92;u002D'</code>) to indicate a negative value or an
  84      * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
  85      * indicate a positive value.  The resulting {@code short} value
  86      * is returned.
  87      *
  88      * <p>An exception of type {@code NumberFormatException} is
  89      * thrown if any of the following situations occurs:
  90      * <ul>
  91      * <li> The first argument is {@code null} or is a string of
  92      * length zero.
  93      *
  94      * <li> The radix is either smaller than {@link
  95      * java.lang.Character#MIN_RADIX} or larger than {@link
  96      * java.lang.Character#MAX_RADIX}.
  97      *
  98      * <li> Any character of the string is not a digit of the
  99      * specified radix, except that the first character may be a minus
 100      * sign {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
 101      * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
 102      * string is longer than length 1.
 103      *
 104      * <li> The value represented by the string is not a value of type
 105      * {@code short}.
 106      * </ul>
 107      *
 108      * @param s         the {@code String} containing the
 109      *                  {@code short} representation to be parsed
 110      * @param radix     the radix to be used while parsing {@code s}
 111      * @return          the {@code short} represented by the string
 112      *                  argument in the specified radix.
 113      * @throws          NumberFormatException If the {@code String}
 114      *                  does not contain a parsable {@code short}.
 115      */
 116     public static short parseShort(String s, int radix)
 117         throws NumberFormatException {
 118         int i = Integer.parseInt(s, radix);
 119         if (i < MIN_VALUE || i > MAX_VALUE)
 120             throw new NumberFormatException(
 121                 "Value out of range. Value:\"" + s + "\" Radix:" + radix);
 122         return (short)i;
 123     }
 124 
 125     /**
 126      * Parses the string argument as a signed decimal {@code
 127      * short}. The characters in the string must all be decimal
 128      * digits, except that the first character may be an ASCII minus
 129      * sign {@code '-'} (<code>'&#92;u002D'</code>) to indicate a
 130      * negative value or an ASCII plus sign {@code '+'}
 131      * (<code>'&#92;u002B'</code>) to indicate a positive value.  The
 132      * resulting {@code short} value is returned, exactly as if the
 133      * argument and the radix 10 were given as arguments to the {@link
 134      * #parseShort(java.lang.String, int)} method.
 135      *
 136      * @param s a {@code String} containing the {@code short}
 137      *          representation to be parsed
 138      * @return  the {@code short} value represented by the
 139      *          argument in decimal.
 140      * @throws  NumberFormatException If the string does not
 141      *          contain a parsable {@code short}.
 142      */
 143     public static short parseShort(String s) throws NumberFormatException {
 144         return parseShort(s, 10);
 145     }
 146 
 147     /**
 148      * Returns a {@code Short} object holding the value
 149      * extracted from the specified {@code String} when parsed
 150      * with the radix given by the second argument. The first argument
 151      * is interpreted as representing a signed {@code short} in




  63 
  64     /**
  65      * Returns a new {@code String} object representing the
  66      * specified {@code short}. The radix is assumed to be 10.
  67      *
  68      * @param s the {@code short} to be converted
  69      * @return the string representation of the specified {@code short}
  70      * @see java.lang.Integer#toString(int)
  71      */
  72     public static String toString(short s) {
  73         return Integer.toString((int)s, 10);
  74     }
  75 
  76     /**
  77      * Parses the string argument as a signed {@code short} in the
  78      * radix specified by the second argument. The characters in the
  79      * string must all be digits, of the specified radix (as
  80      * determined by whether {@link java.lang.Character#digit(char,
  81      * int)} returns a nonnegative value) except that the first
  82      * character may be an ASCII minus sign {@code '-'}
  83      * ({@code '\u005Cu002D'}) to indicate a negative value or an
  84      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
  85      * indicate a positive value.  The resulting {@code short} value
  86      * is returned.
  87      *
  88      * <p>An exception of type {@code NumberFormatException} is
  89      * thrown if any of the following situations occurs:
  90      * <ul>
  91      * <li> The first argument is {@code null} or is a string of
  92      * length zero.
  93      *
  94      * <li> The radix is either smaller than {@link
  95      * java.lang.Character#MIN_RADIX} or larger than {@link
  96      * java.lang.Character#MAX_RADIX}.
  97      *
  98      * <li> Any character of the string is not a digit of the
  99      * specified radix, except that the first character may be a minus
 100      * sign {@code '-'} ({@code '\u005Cu002D'}) or plus sign
 101      * {@code '+'} ({@code '\u005Cu002B'}) provided that the
 102      * string is longer than length 1.
 103      *
 104      * <li> The value represented by the string is not a value of type
 105      * {@code short}.
 106      * </ul>
 107      *
 108      * @param s         the {@code String} containing the
 109      *                  {@code short} representation to be parsed
 110      * @param radix     the radix to be used while parsing {@code s}
 111      * @return          the {@code short} represented by the string
 112      *                  argument in the specified radix.
 113      * @throws          NumberFormatException If the {@code String}
 114      *                  does not contain a parsable {@code short}.
 115      */
 116     public static short parseShort(String s, int radix)
 117         throws NumberFormatException {
 118         int i = Integer.parseInt(s, radix);
 119         if (i < MIN_VALUE || i > MAX_VALUE)
 120             throw new NumberFormatException(
 121                 "Value out of range. Value:\"" + s + "\" Radix:" + radix);
 122         return (short)i;
 123     }
 124 
 125     /**
 126      * Parses the string argument as a signed decimal {@code
 127      * short}. The characters in the string must all be decimal
 128      * digits, except that the first character may be an ASCII minus
 129      * sign {@code '-'} ({@code '\u005Cu002D'}) to indicate a
 130      * negative value or an ASCII plus sign {@code '+'}
 131      * ({@code '\u005Cu002B'}) to indicate a positive value.  The
 132      * resulting {@code short} value is returned, exactly as if the
 133      * argument and the radix 10 were given as arguments to the {@link
 134      * #parseShort(java.lang.String, int)} method.
 135      *
 136      * @param s a {@code String} containing the {@code short}
 137      *          representation to be parsed
 138      * @return  the {@code short} value represented by the
 139      *          argument in decimal.
 140      * @throws  NumberFormatException If the string does not
 141      *          contain a parsable {@code short}.
 142      */
 143     public static short parseShort(String s) throws NumberFormatException {
 144         return parseShort(s, 10);
 145     }
 146 
 147     /**
 148      * Returns a {@code Short} object holding the value
 149      * extracted from the specified {@code String} when parsed
 150      * with the radix given by the second argument. The first argument
 151      * is interpreted as representing a signed {@code short} in