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

Discussion

What will be the output of the program?

public class Case22
{
    public static void main(String[] args) throws Exception
    {
        Integer x = 1000;
        int y = 1000;
        System.out.println(x == y);
    }
}

  • A.The program prints false
  • B.The program prints true
  • C.Compilation fails.
  • D.A NullPointerException is thrown.

Answer: B

Comparing a wrapper with a primitive unboxes the wrapper first, so this is an int comparison of 1000 with 1000 and the answer is true. The cache is irrelevant because no reference comparison happens. Only wrapper against wrapper compares identity.

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

Post your comment

Please log in or register to post a comment.