Java · Question 13 · Hard

What will be the output of the following Java program (default JVM settings)?

public class Main {
    public static void main(String[] args) {
        Integer a = 127, b = 127;
        Integer c = 128, d = 128;
        System.out.println((a == b) + " " + (c == d));
    }
}
  1. true false
  2. true true
  3. false false
  4. false true
Answer and explanation

Answer: A. true false

Explanation: Autoboxing uses Integer.valueOf, which returns cached objects for -128 to 127, so a and b are the same object. 128 is outside the default cache, so c and d are different objects and == is false.

Previous: What will be the output of the following Java program? · Next: What will be the output of the following Java program?

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge