Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Integer a1=4;
Integer a2=4;
System.out.println(a1==a2);
When I debug this code, I can easily see that the values are in the same object.But I don't know why java stores these values in the same object? I want to see what is going on in the back.


What I have tried:

I want to understand this happening.
Posted
Updated 18-Feb-21 3:08am
Comments
Richard MacCutchan 18-Feb-21 9:35am    
What you think you are seeing is not what you actually see. In the above code a1 and a2 have the same value but they are different objects. If you run "javap -v <classname> on your class it will clearly show they are different objects.
[no name] 18-Feb-21 9:39am    
sir, a1 and a2 is the same object. Because When we value it without writing new, java itself does not create a new object, because the limit is 127 Integer classes.
Richard MacCutchan 18-Feb-21 9:50am    
Sorry, I don't understand what that means.

1 solution

No, Integer is a primitive - a value type rather than a reference type.
Introduction to Java Primitives | Baeldung[^]

So the == operator compares the content, not the reference, because there is no reference to a primitive!
You can prove this very easily:
Java
Integer a1=4;
Integer a2=a1;
System.out.println(a1==a2);
a1++;
System.out.println(a1==a2);
a1--;
System.out.println(a1==a2);
If a1 and a2 had the same reference, you would get true, true, true, not true, false, true.
 
Share this answer
 
Comments
[no name] 18-Feb-21 9:25am    
you didnt understand me.
OriginalGriff 18-Feb-21 10:04am    
No, you don't understand what is going on.
a1 and a2 aren't "the same object" they are primitives: they aren't stored in objects on the heap, they are stored on the stack directly, and are no related.
When you assign a value to a primitive, it affects that variable only, not any others.
The == operator knows this, and compares the content not a reference. Follorw teh link I gave you and read that!
[no name] 18-Feb-21 11:03am    
Bro, have ever try to write code like wrapper type? in above section a1 store a reference adress of Integer object. if you don't belive me than try to write in ide and debug this code. you can see that in output a1 and a2 is the same object.
OriginalGriff 18-Feb-21 11:35am    
No, it doesn't. *It's a primitive*
OriginalGriff 18-Feb-21 11:57am    
Let's try an experiment to see what would happen if your were right, and I was wrong.
I give you 4 apples, and I then give you 4 bananas.
The two counts are the same, yes?

But ... if you eat one apple, does that affect the number of bananas?
Of course not: that would be silly!

Now let's change the names of your variables to reflect this:
Integer noApples = 4;
Integer noBananas=noApples;

If you eat an apple, we reduce the number of apples you are holding:
noApples--;

How many bananas are you holding?
Print them using System.out.println and you will see:
System.out.println(noApples == noBananas);
System.out.println(noApples);
System.out.println(noBananas);
Did you get what I said, or what you said?
Because if assigning an Integer to another Integer copied a reference, then the number of bananas will remain the same as the number of apples.

But that isn't what you get, is it:
false
3
4
And that's because Integer is a primitive: when you use it you get a copy of the value, not a reference to it. And if you think about it, there is no other sensible way it can work!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900