When we assign an integer value to an Integer object, the value is autoboxed into an Integer object. For example the statement “Integer x = 10” creates an object ‘x’ with value 10.
Following are some interesting output questions based on comparison of Autoboxed Integer objects.
Predict the output of following Java Program
// file name: Main.java public class Main { public static void main(String args[]) { Integer x = 400, y = 400; if (x == y) System.out.println("Same"); else System.out.println("Not Same"); } } |
Output:
Not Same
Since x and y refer to different objects, we get the output as “Not Same”
The output of following program is a surprise from Java.
// file name: Main.java public class Main { public static void main(String args[]) { Integer x = 40, y = 40; if (x == y) System.out.println("Same"); else System.out.println("Not Same"); } } |
Output:
Same
In Java, values from -128 to 127 are cached, so the same objects are returned. The implementation of valueOf() uses cached objects if the value is between -128 to 127.
If we explicitly create Integer objects using new operator, we get the output as “Not Same”. See the following Java program. In the following program, valueOf() is not used.
// file name: Main.java public class Main { public static void main(String args[]) { Integer x = new Integer(40), y = new Integer(40); if (x == y) System.out.println("Same"); else System.out.println("Not Same"); } } |
Output:
Not Same
Predict the output of the following program. This example is contributed by Bishal Dubey.
class GFG { public static void main(String[] args) { Integer X = new Integer(10); Integer Y = 10; // Due to auto-boxing, a new Wrapper object // is created which is pointed by Y System.out.println(X == Y); } } |
Output:
false
Explanation: Two objects will be created here. First object which is pointed by X due to calling of new operator and second object will be created because of Auto-boxing.
This article is compiled by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to Convert Set of Integer to Array of Integer in Java
- Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
- Comparison of static keyword in C++ and Java
- Comparison of Inheritance in C++ and Java
- Comparison of boolean data type in C++ and Java
- Image Processing in Java | Set 14 ( Comparison of two images )
- Comparison of double and float primitive types in Java
- Comparison of yield(), join() and sleep() in Java
- Java 11 - Features and Comparison
- Comparison of Java with other programming languages
- Comparison of Exception Handling in C++ and Java
- Brief Overview & Comparison of Object-Oriented Programming from C to Java
- Integer.valueOf() vs Integer.parseInt() with Examples
- Java.util.Objects class in Java
- Comparison of Python with Other Programming Languages
- How are Java objects stored in memory?
- How to swap or exchange objects in Java?
- Passing and Returning Objects in Java
- Different ways to create objects in Java
- equals() on String and StringBuffer objects in Java

