I got the following code from a Java book that didn't have explanation about the results. I ran it and got the results noted below inline, but I need help understanding the results:
1. Integer i = -10;
2. Integer j = -10;
3. System.out.print(i==j); // ==> true
4. System.out.print(i.equals(j)); // ==> true
5. Integer n = 128;
6. Integer m = 128;
7. System.out.print(n==m); // ==> false
8. System.out.print(n.equals(m)); // ==> true
My questions are:
- why does line #3 resolve to true? are they not two separate objects?
- if line #3 is true, why does line #7 not true?
Thanks if advance.
Integerclass.