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

Discussion

What will be the output of the program?

public class Listing73
{
    public static void main(String[] args)
    {
        Long a = 127L;
        Long b = 127L;
        System.out.println((a == b) + " " + a.equals(127L) + " " + a.equals(127));
    }
}

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

Answer: A

Long caches the same -128 to 127 range as Integer, so the identity test is true. equals against a long literal boxes it to Long and matches. equals against the int literal 127 boxes it to Integer, and a Long is never equal to an Integer whatever the value, so that one is false.

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

Post your comment

Please log in or register to post a comment.