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

Discussion

The program is run with java -ea Item48 - what will be the output?

public class Item48
{
    public static void main(String[] args)
    {
        Long x = 200L;
        Long y = 200L;
        assert x == y : "not cached";
        System.out.println(x.equals(y));
    }
}

  • A.The program prints false
  • B.Compilation fails.
  • C.The program prints true
  • D.An AssertionError with message not cached

Answer: D

200 is outside the guaranteed cache range of -128 to 127, which applies to Long just as it does to Integer, so each boxing conversion may produce a new object. The identity test is false and the assertion throws. The equals comparison in the unreached print would have been true.

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

Post your comment

Please log in or register to post a comment.