< prev index next >

jdk/src/java.base/share/classes/java/lang/Integer.java

Print this page




 699      * <p>The method does not take steps to guard against the
 700      * {@code CharSequence} being mutated while parsing.
 701      *
 702      * @param      s   the {@code CharSequence} containing the {@code int}
 703      *                  representation to be parsed
 704      * @param      beginIndex   the beginning index, inclusive.
 705      * @param      endIndex     the ending index, exclusive.
 706      * @param      radix   the radix to be used while parsing {@code s}.
 707      * @return     the signed {@code int} represented by the subsequence in
 708      *             the specified radix.
 709      * @throws     NullPointerException  if {@code s} is null.
 710      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 711      *             negative, or if {@code beginIndex} is greater than
 712      *             {@code endIndex} or if {@code endIndex} is greater than
 713      *             {@code s.length()}.
 714      * @throws     NumberFormatException  if the {@code CharSequence} does not
 715      *             contain a parsable {@code int} in the specified
 716      *             {@code radix}, or if {@code radix} is either smaller than
 717      *             {@link java.lang.Character#MIN_RADIX} or larger than
 718      *             {@link java.lang.Character#MAX_RADIX}.
 719      * @since  1.9
 720      */
 721     public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
 722                 throws NumberFormatException {
 723         s = Objects.requireNonNull(s);
 724 
 725         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 726             throw new IndexOutOfBoundsException();
 727         }
 728         if (radix < Character.MIN_RADIX) {
 729             throw new NumberFormatException("radix " + radix +
 730                                             " less than Character.MIN_RADIX");
 731         }
 732         if (radix > Character.MAX_RADIX) {
 733             throw new NumberFormatException("radix " + radix +
 734                                             " greater than Character.MAX_RADIX");
 735         }
 736 
 737         boolean negative = false;
 738         int i = beginIndex;
 739         int limit = -Integer.MAX_VALUE;


 882      * <p>The method does not take steps to guard against the
 883      * {@code CharSequence} being mutated while parsing.
 884      *
 885      * @param      s   the {@code CharSequence} containing the unsigned
 886      *                 {@code int} representation to be parsed
 887      * @param      beginIndex   the beginning index, inclusive.
 888      * @param      endIndex     the ending index, exclusive.
 889      * @param      radix   the radix to be used while parsing {@code s}.
 890      * @return     the unsigned {@code int} represented by the subsequence in
 891      *             the specified radix.
 892      * @throws     NullPointerException  if {@code s} is null.
 893      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 894      *             negative, or if {@code beginIndex} is greater than
 895      *             {@code endIndex} or if {@code endIndex} is greater than
 896      *             {@code s.length()}.
 897      * @throws     NumberFormatException  if the {@code CharSequence} does not
 898      *             contain a parsable unsigned {@code int} in the specified
 899      *             {@code radix}, or if {@code radix} is either smaller than
 900      *             {@link java.lang.Character#MIN_RADIX} or larger than
 901      *             {@link java.lang.Character#MAX_RADIX}.
 902      * @since  1.9
 903      */
 904     public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
 905                 throws NumberFormatException {
 906         s = Objects.requireNonNull(s);
 907 
 908         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 909             throw new IndexOutOfBoundsException();
 910         }
 911         int start = beginIndex, len = endIndex - beginIndex;
 912 
 913         if (len > 0) {
 914             char firstChar = s.charAt(start);
 915             if (firstChar == '-') {
 916                 throw new
 917                     NumberFormatException(String.format("Illegal leading minus sign " +
 918                                                        "on unsigned string %s.", s));
 919             } else {
 920                 if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
 921                         (radix == 10 && len <= 9)) { // Integer.MAX_VALUE in base 10 is 10 digits
 922                     return parseInt(s, start, start + len, radix);


1002      * </blockquote>
1003      *
1004      * @param      s   the string to be parsed.
1005      * @return     an {@code Integer} object holding the value
1006      *             represented by the string argument.
1007      * @exception  NumberFormatException  if the string cannot be parsed
1008      *             as an integer.
1009      */
1010     public static Integer valueOf(String s) throws NumberFormatException {
1011         return Integer.valueOf(parseInt(s, 10));
1012     }
1013 
1014     /**
1015      * Cache to support the object identity semantics of autoboxing for values between
1016      * -128 and 127 (inclusive) as required by JLS.
1017      *
1018      * The cache is initialized on first usage.  The size of the cache
1019      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
1020      * During VM initialization, java.lang.Integer.IntegerCache.high property
1021      * may be set and saved in the private system properties in the
1022      * jdk.internal.misc.VM class.
1023      */
1024 
1025     private static class IntegerCache {
1026         static final int low = -128;
1027         static final int high;
1028         static final Integer cache[];
1029 
1030         static {
1031             // high value may be configured by property
1032             int h = 127;
1033             String integerCacheHighPropValue =
1034                 VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
1035             if (integerCacheHighPropValue != null) {
1036                 try {
1037                     int i = parseInt(integerCacheHighPropValue);
1038                     i = Math.max(i, 127);
1039                     // Maximum array size is Integer.MAX_VALUE
1040                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
1041                 } catch( NumberFormatException nfe) {
1042                     // If the property cannot be parsed into an int, ignore it.
1043                 }
1044             }
1045             high = h;
1046 
1047             cache = new Integer[(high - low) + 1];
1048             int j = low;
1049             for(int k = 0; k < cache.length; k++)
1050                 cache[k] = new Integer(j++);
1051 
1052             // range [-128, 127] must be interned (JLS7 5.1.7)
1053             assert IntegerCache.high >= 127;
1054         }




 699      * <p>The method does not take steps to guard against the
 700      * {@code CharSequence} being mutated while parsing.
 701      *
 702      * @param      s   the {@code CharSequence} containing the {@code int}
 703      *                  representation to be parsed
 704      * @param      beginIndex   the beginning index, inclusive.
 705      * @param      endIndex     the ending index, exclusive.
 706      * @param      radix   the radix to be used while parsing {@code s}.
 707      * @return     the signed {@code int} represented by the subsequence in
 708      *             the specified radix.
 709      * @throws     NullPointerException  if {@code s} is null.
 710      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 711      *             negative, or if {@code beginIndex} is greater than
 712      *             {@code endIndex} or if {@code endIndex} is greater than
 713      *             {@code s.length()}.
 714      * @throws     NumberFormatException  if the {@code CharSequence} does not
 715      *             contain a parsable {@code int} in the specified
 716      *             {@code radix}, or if {@code radix} is either smaller than
 717      *             {@link java.lang.Character#MIN_RADIX} or larger than
 718      *             {@link java.lang.Character#MAX_RADIX}.
 719      * @since  9
 720      */
 721     public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
 722                 throws NumberFormatException {
 723         s = Objects.requireNonNull(s);
 724 
 725         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 726             throw new IndexOutOfBoundsException();
 727         }
 728         if (radix < Character.MIN_RADIX) {
 729             throw new NumberFormatException("radix " + radix +
 730                                             " less than Character.MIN_RADIX");
 731         }
 732         if (radix > Character.MAX_RADIX) {
 733             throw new NumberFormatException("radix " + radix +
 734                                             " greater than Character.MAX_RADIX");
 735         }
 736 
 737         boolean negative = false;
 738         int i = beginIndex;
 739         int limit = -Integer.MAX_VALUE;


 882      * <p>The method does not take steps to guard against the
 883      * {@code CharSequence} being mutated while parsing.
 884      *
 885      * @param      s   the {@code CharSequence} containing the unsigned
 886      *                 {@code int} representation to be parsed
 887      * @param      beginIndex   the beginning index, inclusive.
 888      * @param      endIndex     the ending index, exclusive.
 889      * @param      radix   the radix to be used while parsing {@code s}.
 890      * @return     the unsigned {@code int} represented by the subsequence in
 891      *             the specified radix.
 892      * @throws     NullPointerException  if {@code s} is null.
 893      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 894      *             negative, or if {@code beginIndex} is greater than
 895      *             {@code endIndex} or if {@code endIndex} is greater than
 896      *             {@code s.length()}.
 897      * @throws     NumberFormatException  if the {@code CharSequence} does not
 898      *             contain a parsable unsigned {@code int} in the specified
 899      *             {@code radix}, or if {@code radix} is either smaller than
 900      *             {@link java.lang.Character#MIN_RADIX} or larger than
 901      *             {@link java.lang.Character#MAX_RADIX}.
 902      * @since  9
 903      */
 904     public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
 905                 throws NumberFormatException {
 906         s = Objects.requireNonNull(s);
 907 
 908         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 909             throw new IndexOutOfBoundsException();
 910         }
 911         int start = beginIndex, len = endIndex - beginIndex;
 912 
 913         if (len > 0) {
 914             char firstChar = s.charAt(start);
 915             if (firstChar == '-') {
 916                 throw new
 917                     NumberFormatException(String.format("Illegal leading minus sign " +
 918                                                        "on unsigned string %s.", s));
 919             } else {
 920                 if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
 921                         (radix == 10 && len <= 9)) { // Integer.MAX_VALUE in base 10 is 10 digits
 922                     return parseInt(s, start, start + len, radix);


1002      * </blockquote>
1003      *
1004      * @param      s   the string to be parsed.
1005      * @return     an {@code Integer} object holding the value
1006      *             represented by the string argument.
1007      * @exception  NumberFormatException  if the string cannot be parsed
1008      *             as an integer.
1009      */
1010     public static Integer valueOf(String s) throws NumberFormatException {
1011         return Integer.valueOf(parseInt(s, 10));
1012     }
1013 
1014     /**
1015      * Cache to support the object identity semantics of autoboxing for values between
1016      * -128 and 127 (inclusive) as required by JLS.
1017      *
1018      * The cache is initialized on first usage.  The size of the cache
1019      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
1020      * During VM initialization, java.lang.Integer.IntegerCache.high property
1021      * may be set and saved in the private system properties in the
1022      * sun.misc.VM class.
1023      */
1024 
1025     private static class IntegerCache {
1026         static final int low = -128;
1027         static final int high;
1028         static final Integer cache[];
1029 
1030         static {
1031             // high value may be configured by property
1032             int h = 127;
1033             String integerCacheHighPropValue =
1034                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
1035             if (integerCacheHighPropValue != null) {
1036                 try {
1037                     int i = parseInt(integerCacheHighPropValue);
1038                     i = Math.max(i, 127);
1039                     // Maximum array size is Integer.MAX_VALUE
1040                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
1041                 } catch( NumberFormatException nfe) {
1042                     // If the property cannot be parsed into an int, ignore it.
1043                 }
1044             }
1045             high = h;
1046 
1047             cache = new Integer[(high - low) + 1];
1048             int j = low;
1049             for(int k = 0; k < cache.length; k++)
1050                 cache[k] = new Integer(j++);
1051 
1052             // range [-128, 127] must be interned (JLS7 5.1.7)
1053             assert IntegerCache.high >= 127;
1054         }


< prev index next >