The Difference between parseInt and valueOf of Integer

Let's take a look at the following code

String s = "1";
System.out.println(Integer.valueOf(s));
System.out.println(Integer.parseInt(s));

What is the output? Yes, the same is 1. Both methods can convert numeric strings to int-type integers, but there i s a difference between the two methods. The valueOf(String s) method calls the parseInt(String s, int radix) method, while the parseInt(String s, int radix) method returns an int-type value, and then calls the valueOf (int) method. The int is boxed and returned to the packaging type Integer.

So if you don't need to return the wrapper type, you can call the parseInt (String) method directly, which is more efficient.

The following section is the source code for the valueOf (String) method

public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}

You can see that when you call the parseInt method, you also pass an int parameter radix, which represents the decimal system and is converted by default. The following is the source code of the method, and I annotate some comments.

public static int parseInt(String s, int radix)
throws NumberFormatException
{

/* Warning: Before initializing IntegerCache, this method may be called in advance during VM initialization. You must be careful not to use the valueOf method.
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/

//NumberFormatException is thrown if the string is empty
if (s == null) {
throw new NumberFormatException("null");
}
//Pass process parameter less than 2, throw NumberFormatException, and prompt base less than minimum base
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
//Pass in a process parameter less than 36, throw NumberFormatException, and indicate that the base is less than the maximum.
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
}

int result = 0;
//Negative is used to determine whether the result is negative.
boolean negative = false;
//Get string length
int i = 0, len = s.length();
//limit = -2147483647
int limit = -Integer.MAX_VALUE;
//Value used to determine whether an overflow occurs before adding the next digit
int multmin;
int digit;

//Character length greater than 0
if (len > 0) {
//Determine whether a string has symbols
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
//The first sign is a negative sign, so the result is a negative number.
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
//Throw NumberFormatException instead of negative or positive
throw NumberFormatException.forInputString(s);

//Length 1, throw NumberFormatException
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
//When calculating multmin, note that the limit of the negative number is different from that of the positive number. The limit of the negative number is - 2147483648, and the limit of the positive number is - 2147483647.
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
//The character is not empty, but the length of the character is equal to 0, throwing NumberFormatException
throw NumberFormatException.forInputString(s);
}
//Judging whether the result is positive or negative according to the sign of positive and negative numbers
return negative ? result : -result;
} Tags: Programming less

Posted on Tue, 08 Oct 2019 23:12:31 -0700 by Fender963