What will be the output of the program?
public class Trial21
{
Trial21()
{
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println((a == b) + " " + (c == d));
}
public static void main(String[] args)
{
new Trial21();
}
}
Answer: A
Autoboxing an int between -128 and 127 must return a cached Integer, so a and b reference the same object and the first test is true. 128 is outside that guaranteed range, so each boxing may create a new object and the second is false. equals would be true in both cases.
Please log in or register to report this question.