Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How this is possible could anyone tell it

 
arvind kushwaha
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear team,

Please explain the below code why it showing different result being the same object.

Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // FALSE

Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE

Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE

Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE

int jjj1 = 127;
Integer jjj2 = 127;
System.out.println(jjj1 == jjj2); // TRUE

Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE

Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE

Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1 == w2); // TRUE

Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1 == m2); // FALSE

int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE

Could any one explain it properly as i m not able to understand it.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please check this post.

Thanks
 
arvind kushwaha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks vijay...

int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE

i understand above all statement but still having doubt in above statement. I am not get it. from my point of view it should be false but when i run the code it turn out true..

Please suggest me ....How its working

 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If one value is a primitive and the other is an object, the object will be unboxed into a primitive, not the other way around. Just try setting mm2 to null and try again.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arvind kushwaha wrote:Dear team,

Please explain the below code why it showing different result being the same object.

Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // FALSE

Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE

Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE

Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE

int jjj1 = 127;
Integer jjj2 = 127;
System.out.println(jjj1 == jjj2); // TRUE

Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE

Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE

Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1 == w2); // TRUE

Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1 == m2); // FALSE

int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE

Could any one explain it properly as i m not able to understand it.



keep this in mind. to save memory two wrapper objects will be equal if

1. they are of the type Boolean, Byte
2. they are of type Short, Integer and is between -128 to 127.

also this is only true for when you are comparing two wrapper objects. i.e. if you are comparing wrapper with primitives then it won't hold, because in that case wrapper will be unboxed to primitive and primitive comparisons will take place which are as usual.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:2. they are of type Short, Integer and is between -128 to 127.

In the case of Integer the upper limit can be extended. 127 is the default and minimum value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian is correct and the previous post quoted is incorrect. The range -128…127 is described as a minimum in the Java Language Specification (JLS) and the Integer class. It says in the JLS,
Ideally, boxing a given primitive value p, would always yield an identical reference.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, which implementation are you using which caches Integer.valueOf(128)?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way, which implementation are you using which caches Integer.valueOf(128)?

His example wasn't caching 128. When both variables were Integers it returned false. When one variable was an int it returned true because the Integer variable would have been unboxed.

arvind kushwaha wrote:
Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE

Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Burkett wrote: . . . His example wasn't caching 128. When both variables were Integers it returned false. When one variable was an int it returned true because the Integer variable would have been unboxed. . . .
Damn! I got that bit wrong.
I was hoping somebody had found an implementation which caches more than -128…127!
 
Junilu Lacar
Bartender
Posts: 7569
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bottom line: apart from academic exercises like the above, for all practical intents and purposes always use equals() when comparing wrapper classes and classes in general for equality. That way, you don't have to know all the gory details of the Java specifications and, more importantly, neither do any of the poor saps who have to maintain your code.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I was hoping somebody had found an implementation which caches more than -128…127!

This guy seems to have found one, but I couldn't reproduce on my system. -XX:AutoBoxCacheMax is reported as unrecognized VM option for me
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ça marche bien sur Java-sept.

or . . . that works nicely on Java7.

It says that option was introduced in Java6.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It says that option was introduced in Java6.

No luck with Java 6:

The same with 1.6.0_32. If it ever worked with Java 6, it must have been "unintroduced".
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously strange French arithmetic.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something is amiss here. The JDK6 Integer class source code does mention the AutoBoxCacheMax parameter and seemingly provides some support for dynamic setting of the cache size...
 
Adam Zehavi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arvind kushwaha wrote:
Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // FALSE

Two different new instances... so false!

arvind kushwaha wrote:
Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE

boxed --> -128 <= value < 128 --> unboxed --> compare primitive... so true!

arvind kushwaha wrote:
Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE

boxed --> value < -128 || value >= 128 --> compare two different instances... so false!

arvind kushwaha wrote:
Integer jj1 = 127;
Integer jj2 = 127;
System.out.println(jj1 == jj2); // TRUE

boxed --> -128 <= value < 128 --> unboxed --> compare primitive... so true!

arvind kushwaha wrote:
Integer kk1 = 128;
Integer kk2 = 128;
System.out.println(kk1 == kk2); // FALSE

value < -128 || value >= 128 --> compare two different instances... so false!

arvind kushwaha wrote:
Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // TRUE

has primitive in the condition --> wrapper unboxed --> compare primitive... so true!

arvind kushwaha wrote:
Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1 == w2); // TRUE

boxed --> -128 <= value < 128 --> unboxed --> compare primitive... so true!

arvind kushwaha wrote:
Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1 == m2); // FALSE

boxed --> value < -128 || value >= 128 --> compare two different instances... so false!

arvind kushwaha wrote:
int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // TRUE

has primitive in the condition --> wrapper unboxed --> compare primitive... so true!

arvind kushwaha wrote:
int jjj1 = 127;
Integer jjj2 = 127;
System.out.println(jjj1 == jjj2); // TRUE

has primitive in the condition --> wrapper unboxed --> compare primitive... so true!
boxed --> -128 <= value < 128 --> unboxed --> compare primitive... so true!

Which of the last one you think is the cause?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Zehavi wrote: . . .
arvind kushwaha wrote:
Integer j1 = 2;
Integer j2 = 2;
System.out.println(j1 == j2); // TRUE

boxed --> -128 <= value < 128 --> unboxed --> compare primitive... so true!

arvind kushwaha wrote:
Integer k1 = 150;
Integer k2 = 150;
System.out.println(k1 == k2); // FALSE

boxed --> value < -128 || value >= 128 --> compare two different instances... so false!
. . .
Are you sure about that? The details are in the Java Language Specification. It doesn’t say anything about boxing or unboxing when using == and != on reference types, so doesn’t the true output simply mean that caching causes boxing to reuse the existing object?
And using 150 only returns false if values of 150 are not cached. We have already seen discussion about size of the cache.
 
Adam Zehavi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Are you sure about that? The details are in the Java Language Specification. It doesn’t say anything about boxing or unboxing when using == and != on reference types, so doesn’t the true output simply mean that caching causes boxing to reuse the existing object?


Good point, I was not clear about that, -128 <= value < 128 means, that it uses the byte value cache, and reuse the instance of the wrapper, while 150 causes a new instance every time.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried the flag as well, but -XX:AutoBoxCacheMax only works with 64-bit JVMs. With both Java 6u32 and Java 7u4, I get an error with the 32-bit version and it works with the 64-bit version (yes I have 4 JVMs installed).

That said, using -Djava.lang.Integer.IntegerCache.high=XXX works in all four versions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic