Machine-translated by Google. Questions are authored in English.
Top

Discussion

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();
    }
}

  • A.The program prints true false
  • B.The program prints false true
  • C.The program prints false false
  • D.The program prints true true

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.