List<Integer> li= Arrays.asList(106,106,248,248,286,286,344,357,357);
System.out.println(li.get(4) == li.get(5));
System.out.println(li.get(0) == li.get(1));
Why first one print false and second print true
List<Integer> li= Arrays.asList(106,106,248,248,286,286,344,357,357);
System.out.println(li.get(4) == li.get(5));
System.out.println(li.get(0) == li.get(1));
Why first one print false and second print true
The primitive type int has been boxed into Integer objects prior to being added into the ArrayList
That explains why your comparisons might fail. The equality operator will result in true if the objects are the same. You could have the same integer values boxed into two different Integer objects and consequently the equal operator will result to false.
"To compare two Integer objects in Java, you can use the equals() method. The equals() method compares the value of the two objects and returns true if they are equal, or false if they are not. In this example, the equals() method is used to compare the num1 and num2 Integer objects."