I want to compare two elements in an ArrayList == operator
, I write this code:
ArrayList<Integer> myArray=new ArrayList<>();
myArray.add(-128);
myArray.add(-128);
System.out.println(myArray.get(0)==myArray.get(1));
I get true
as result, but with other value like:
myArray.add(2000);
myArray.add(2000);
System.out.println(myArray.get(0)==myArray.get(1));
I get false
as result.
I know that == operator
Compares references, not values, in this case the two Integers have different references, so logically we must have false
for both case.
I don't understand Why I get true
for only an Integer
from -128 to 127
and false
for other Integer
.
NB: I see many questions in Stackoverflow and Google, but I can't find an answer of my question