Differences between java Integer. valueOf and Integer. parseInt and new Integer and precautions: parseint and valueof
First, let's look at the following results under explain.
1.System.out.println(127==127); //true , int type compare2.System.out.println(128==128); //true , int type compare3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare4.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, int type compare5.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,object compare, because IntegerCache return a same object6.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,object compare, because number beyond the IntegerCache7.System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); //true , int type compare
Explanation
When int type constants are compared, = is a value comparison, so returns true. 1 and 2 are value comparison.
New Integer () constructs a new Integer object each time, so 3 returns false. 3 is object comparison.
Integer. parseInt constructs an int constant each time, so 4 returns true. 4 is the value comparison.
Integer. valueOf returns an Integer object. The default value is-128 ~ Between 127, the existing objects in the cache are returned (if any), So 5 returns true, 6 returns false. 5 and 6 are object comparison.
The first is a special comparison between int and Integer. The result is a Value Comparison and true is returned.
Summary
For integer comparison, first determine whether it is a value comparison or an object comparison. If the value comparison is positive, true is returned. One of them is a value comparison. Compare objects to see how the objects are constructed. If the new Integer method is used, a new object is generated each time. If the two new integers are compared, false is returned. if the valueOf method is used, check whether the value range is-128 ~ Between 127. If yes, the object with the same value constructed is the same object, = true is returned after comparison, otherwise false is returned.
Therefore, there is no problem when comparing values =. For Integer comparison, it is best to use the equals method to compare object content. Of course, check whether Integer is not null.
Knowledge Extension
Analyze the source code of Integer. valueOf
1. The Integer. valueOf method we call. It first calls parseInt to convert it to an int value, and then calls its own overload method.
public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); }
2. the Integer. valueOf overload method determines whether to take an Integer object from the cache based on the value I.
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
3. Integer constructor, which is very simple
public Integer(int value) { this.value = value; }
4. IntegerCache static class, which is an Integer internal class and has three attributes (one cached Integer array + one set of cache ranges)
Private static class IntegerCache {static final int low =-128; static final int high; static final Integer cache []; static {// high value may be configured by property (maximum value configurable) int h = 127; String integerCacheHighPropValue = sun. misc. VM. getSavedProperty ("java. lang. integer. integerCache. high "); // read VM parameter if (integerCacheHighPropValue! = Null) {try {int I = parseInt (integerCacheHighPropValue); // convert the configuration value to the int value I = Math. max (I, 127); // compare with 127, take the greater one // Maximum array size is Integer. MAX_VALUE (controls the size of the cache array. The maximum value is the maximum value of an integer. In this way, the h value must be smaller than the maximum value of an integer because-128 ~ 0) h = Math. min (I, Integer. MAX_VALUE-(-low)-1); // h = Math. min (I, Integer. MAX_VALUE-129), number of positive integers that can be cached} catch (NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it .}} high = h; cache = new Integer [(high-low) + 1]; // construct the cache array int j = low; for (int k = 0; k <cache. length; k ++) cache [k] = new Integer (j ++ ); // cache some int constants into the Integer object array cache. // range [-128,127] must be interned (JLS7 5.1.7) assert IntegerCache. high> = 127; // if the value is less than 127, an exception is thrown.} private IntegerCache (){}}
Read the preceding Integer. after the valueOf source code, you will find that the default Integer cache int constant pool can be configured by adding VM parameters and adding:-Djava. lang. integer. integerCache. high = 200
You can add parameters to the VM Options Option of Intellij IDEA running configuration.