File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How this is possible could anyone tell it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How this is possible could anyone tell it" Watch "How this is possible could anyone tell it" New topic
Author

How this is possible could anyone tell it

arvind kushwaha
Ranch Hand

Joined: Aug 12, 2011
Posts: 32
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

Joined: Nov 04, 2008
Posts: 146

Hi,

Please check this post.

Thanks


The important thing is not to stop questioning.Curiosity has its own reason for existing.
arvind kushwaha
Ranch Hand

Joined: Aug 12, 2011
Posts: 32
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: May 30, 2012
Posts: 679
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
By the way, which implementation are you using which caches Integer.valueOf(128)?
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Feb 26, 2001
Posts: 4445
    
    5

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Oct 13, 2005
Posts: 38033
    
  22
Ça marche bien sur Java-sept.

or . . . that works nicely on Java7.

It says that option was introduced in Java6.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Oct 13, 2005
Posts: 38033
    
  22
Obviously strange French arithmetic.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Jun 03, 2012
Posts: 11
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

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Jun 03, 2012
Posts: 11
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How this is possible could anyone tell it
 
Similar Threads
Wrappers & Primitives Comparision
Wrappers and primitives comparison:
Wrapper and primitives comparison
Java 1.5 Autoboxing parameters
doubt about ==