Understanding Java Integer
-
This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.
-
This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.
The following test program shows what happens:
public class Test {
/\*\* \* @param args \*/ public static void main(String\[\] args) { Integer a; Integer b; a = 10; b = 10; if (a == b) System.out.println("True"); }
}
You can also verify by disassembling the class via
javap -c
.Veni, vidi, abiit domum
-
The following test program shows what happens:
public class Test {
/\*\* \* @param args \*/ public static void main(String\[\] args) { Integer a; Integer b; a = 10; b = 10; if (a == b) System.out.println("True"); }
}
You can also verify by disassembling the class via
javap -c
.Veni, vidi, abiit domum
-
This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.
Raptor81 wrote:
In my experience, seems that Java does reference comparison with two Integers
Does exactly that for equality and inequality. If it didn't it wouldn't be backwards compatible.
Raptor81 wrote:
and seems that it does value comparison with one Integer and one constant.
Yes however the problem with doing that is that one can then forget about equality and inequality. And that becomes even more of a problem because java caches integers in the 0 to 128 range. So simple tests can succeed and then fail in production when bigger values are used. (Keep in mind that the range of integers that are cached is not part of the specification so it can change.) So the best idea is to never rely on the operators in the first place. This is demonstrated with the following.
Integer a1 = 1000; Integer a2 = 1000; Integer a3 = a1; Integer b1 = 10; // Less than 128 so Integer is cached. Integer b2 = 10; System.out.println("1000 is " + (a1 == a2)); System.out.println("10 is " + (b1 == b2)); System.out.println("1000 != is " + (a1 != a3));
-
Richard MacCutchan wrote:
The following test program shows what happens:
Errr...I don't think so. And that is demonstrated by changing the '10' to '1000' and then running the program again.
-
This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.
Integer is not different than any other Java Object (with the previously noted exception that the jvm provides some help to avoid duplication in the smaller values). Perhaps one thing that makes it seem confusing is the auto-conversion between 'int' and Integer. I could see that appearing to be confusing at first. The main thing to remember is that if you want to do numeric comparisons like ==, <, > etc. just make sure you are doing them on 'ints' not Integers.