The test code
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128; System.out.println(i3 == i4); }}Copy the code
The execution result of the above code is:
true false
First, when we compile the above test code into bytecode (.class), the encoded code looks like this:
public class IntegerTest {
public static void main(String[] paramArrayOfString) {
Integer integer1 = Integer.valueOf(127);
Integer integer2 = Integer.valueOf(127);
System.out.println((integer1 == integer2));
Integer integer3 = Integer.valueOf(128);
Integer integer4 = Integer.valueOf(128); System.out.println((integer3 == integer4)); }}Copy the code
ValueOf = valueOf; valueOf = valueOf;
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Copy the code
IntegerCache (); IntegerCache ();
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
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if(integerCacheHighPropValue ! =null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache(a) {}}Copy the code
If the value of Integer is between -128 and 127, it will reuse existing objects, so i1 (127) and i2 will be true when the value of == is used, and i2 will be false when the value of 128 is used.
As a matter of fact, there are corresponding provisions in The Java Development Manual of Alibaba, which are as follows:
【 Force 】 Compare values of all integer wrapped class objects using equals.
Note: For Integer var =? For values between -128 and 127, Integer objects are generated in integercache. cache and reuse existing objects. Integer values in this range can be determined directly by using ==, but all data outside this range will be generated on the heap. It does not reuse existing objects, which is a big pit and is recommended to use equals.
Matters needing attention
Not only that, but when we use new Integer, we can’t compare with ==, no matter what the value is, as shown in the following code:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127); System.out.println(i1 == i2); }}Copy the code
The execution result of the above code is:
false
This is because the new Integer method does not use IntegerCache, but creates a new object directly, and therefore cannot be compared with ==.
Tip: == is used to directly compare references to two objects to see if they have the same value, and equals is used to compare whether two objects have the same value.
Other ways of comparison
compareTo
The Integer class implements the Comparable interface, so we can use compareTo to compare the size of two values.
public final class Integer extends Number implements Comparable<Integer> {
// Ignore everything else
}
Copy the code
CompareTo is used as follows:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(128);
Integer i2 = new Integer(128); System.out.println(i1.compareTo(i2)); }}Copy the code
The execution result of the above code is:
0
CompareTo source code is as follows:
public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
Copy the code
CompareTo returns -1, 0, and 1, where -1 indicates that the first value is less than the second value. 0 means two values are equal; 1 means that the former value is greater than the latter, so we use it to compare whether the values of two Integers are equal.
Direct operation
The compareTo method gives us an inspiration, we can directly subtract two values, if the value of the subtraction is equal to 0, the comparison of the two values are the same, the following example code:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = new Integer(128);
Integer i2 = new Integer(128);
System.out.println((i1 - i2) == 0); }}Copy the code
The execution result of the above code is:
true
intValue
We can use intValue to get an Integer int value and then use == to compare, as shown in the following example:
public class IntegerTest {
public static void main(String[] args) {
Integer i = 558;
Integer i2 = 558; System.out.println(i.intValue() == i2.intValue()); }}Copy the code
The execution result of the above code is:
true
Exclusive or
Xor is a mathematical operator that applies to logical operations. In a computer, if a and B have different values, the xOR result is 1. If a and B have the same value, the xOR result is 0.
Such as:
- 1 or 0 = 1
- 0 or 0 = 0
- 1 or 1 = 0
The xOR implementation example is as follows:
public class IntegerTest {
public static void main(String[] args) {
Integer i = 558;
Integer i2 = 558;
System.out.println((i ^ i2) == 0); }}Copy the code
The execution result of the above code is:
true
Extended knowledge: IntegerCache value field modification
The default value of IntegerCache ranges from -128 to 127, but we can adjust the maximum value of IntegerCache by setting the startup parameter. For example, we can set the vm startup parameter -xx :AutoBoxCacheMax=1000, This configuration indicates that the maximum value of the cache is set to 1000. If Idea is configured as follows:
public class IntegerTest {
public static void main(String[] args) {
Integer i1 = 999;
Integer i2 = 999; System.out.println(i1 == i2); }}Copy the code
The execution result of the above code is:
true
The result of the run shows that the IntegerCache range has been successfully changed.
conclusion
In this article, we introduced six ways to compare Integer: ==, equals, compareTo, and compareTo are not used to compare integers. They only apply to a range of values (-128 to 127) that are not new Integers. The equals comparison is the most common.
Interactive topic
Do you know any other comparisons besides the above ones? Please add your comments in the comments section.
Follow the public account “Java Chinese Community” to subscribe more exciting.