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

Discussion

Which line rests on a mistaken assumption?

public class Sample19
{
    public static void main(String[] args)
    {
        Integer x = 200;
        Integer y = 200;
        assert x == y : "boxes differ";
        System.out.println("cached");
    }
}

  • A.System.out.println("cached");
  • B.Nothing here is mistaken.
  • C.assert x == y : "boxes differ";
  • D.Integer y = 200;

Answer: C

Only values from -128 to 127 are guaranteed to come from the boxing cache, so two Integers holding 200 may be distinct objects and the reference comparison is unreliable. The assertion can pass or fail depending on the value alone. Wrapper values are compared with equals, or by unboxing to int.

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

Post your comment

Please log in or register to post a comment.