I've been learning Java for quite a while and recently stumbled upon a strange result, during Long comparison.
Here is a piece of code:
public class Test {
public static void main(String[] args) {
Long a = 100L;
Long b = 100L;
Long c = 150L;
Long d = 150L;
System.out.println(a == b);
System.out.println(c == d);
}
}
true
false
I know that while Java doesn't support your general comparison between reference types with ==
, it usually caches all constant values that you hardcode and then smart compare them.
And I honestly believed that, until my code crashed. After some tedious debugging I found the bug, and even reproduced it in few lines.
What is going on here?