Originally posted by Manpreet Kaur:
The following question is from http://www.geekevaluation.com/
public class etattva5 {
public static void main(String[] args) {
Integer i = 200;
Integer j = 200;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}
What will be output for the above program?
1. false false
2. false true
3. true true
4. true false
Correct AnswerExplanation
false true
integer prec limit,it will give true true till 127 no after that.
Yeah , what's mentioned is perfectly correct.
Till the point Integer value <= 127 , both the Integer objects will have the same value as well as the same reference.
Only one object is created for both the statements.
However, here you haev Integer = 200.
Which is more then 127 .
In that case , there are separate objects created for both.
Therefore,
System.out.print(i==j);
will be False ( They are not the same objects)
and System.out.print(i.equals(j));
will be True - they have the same value ie.200
Hope that helps.