Integer comparison in Java is tricky, in that int
and Integer
behave differently. I get that part.
But, as this example program shows, (Integer)400
(line #4) behaves differently than (Integer)5
(line #3). Why is this??
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.format("1. 5 == 5 : %b\n", 5 == 5);
System.out.format("2. (int)5 == (int)5 : %b\n", (int)5 == (int)5);
System.out.format("3. (Integer)5 == (Integer)5 : %b\n", (Integer)5 == (Integer)5);
System.out.format("4. (Integer)400 == (Integer)400 : %b\n", (Integer)400 == (Integer)400);
System.out.format("5. new Integer(5) == (Integer)5 : %b\n", new Integer(5) == (Integer)5);
}
}
Result
1. 5 == 5 : true // Expected
2. (int)5 == (int)5 : true // Expected
3. (Integer)5 == (Integer)5 : true // Expected
4. (Integer)400 == (Integer)400 : false // WHAT?
5. new Integer(5) == (Integer)5 : false // Odd, but expected