Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { Integer a = 127, b = 127, c =…

1323Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        Integer a = 127, b = 127, c = 128, d = 128;
        System.out.println((a == b) + " " + (c == d) + " " + c.equals(d));
    }
}
  • Atrue false true
  • Btrue true true
  • Cfalse false true
  • Dtrue false false
Correct answer: A

Explanation

Integer values from −128 to 127 are cached so a == b is true; 128 is not cached, so c == d compares different objects (false), while equals compares values (true).

Discussion 0

Nobody has replied yet. Start the discussion!