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 Wrapper comparison. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Wrapper comparison." Watch "Wrapper comparison." New topic
Author

Wrapper comparison.

Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

Hi Ranchers,

Consider the example shown below:-

class AutoBoxing {


public static void main(String [] args) {

Integer i1 = 1000;
Integer i2 = 1000;

if(i1 != i2) {

System.out.println("Different Objects");
}
if(i1.equals(i2)) {

System.out.println("Meaningfully equal");
}

Integer i11 = 10;
Integer i12 = 10;
if(i11 == i12) {

System.out.println("Same Objects");
}
if(i11.equals(i12)) {

System.out.println("Meaningfully equal");
}

}
}
output:-
Different Objects
Meaningfully equal
Same Objects
Meaningfully equal


Why if(i1 != i2) is true in first case(value 1000 for i1 and i2) and if(i11 == i12) is true in the second (value 10 for i11 and i12) . could anyone explain me?


Thanks in advance.


SCJP 1.4, SCWCD 5
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Hi Niteen,
Integer Wrapper object will always be == when their primitive values are same and that value must fall between -127 to 128.

Can't believe you have done SCJP and SCWCD


Sage of The Monstrous Toad of Mount Myoboku
Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

rohan yadav wrote:Hi Niteen,
Integer Wrapper object will always be == when their primitive values are same and that value must fall between -127 to 128.

Can't believe you have done SCJP and SCWCD



Why is this range defined?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

Your question about == and wrapper objects is a frequently asked question.

See for example this topic for an explanation.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Patil Niteen wrote:Hi Ranchers,

Consider the example shown below:-

class AutoBoxing {


public static void main(String [] args) {

Integer i1 = 1000;
Integer i2 = 1000;

if(i1 != i2) {

System.out.println("Different Objects");
}
if(i1.equals(i2)) {
.....

Please use code tags.


My Blog SCJP 5 SCWCD 5
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
rohan yadav wrote:Hi Niteen,
Integer Wrapper object will always be == when their primitive values are same and that value must fall between -127 to 128.


I would have to disagree with that. I think it is just a by-product of the compiler optimization rather than a guaranteed feature from the language spec. Consider my test code:


}
13
13
true
false
true
[/code]

For String, I know the compiler will create a string pool for all the String literals and use the same instance from the pool if the code is refering to the same String literal. I think it is just the same case for autoboxed Integer wrapper. For manually created wrapper, it is obvious no compiler optimiation has been applied and they are not ==.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Nittin,

If we are autoboxing some value to one of the wrapper classes (Boolean, Byte, Short, Char, Integer, Long), the compiler sometimes creates a new object and sometimes uses the pool of values, similarly like with Strings.

autoboxing to Boolean and Byte always returns an object from the pool
autoboxing to Char, Short, Integer and Long returns an object from the pool when the autoboxed value is between -128 and 127 (inclusive)
autoboxing of Float and Double does not use the pool and always returns a new object


Cheers Munees
MyBlog


Cheers Munees
My Blog
Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

Thanks Jesper Young,

What a great explanation. Most of the books do not contain such explanations.
According to the explanation , the compiler has a cache of all the Integer objects between the range -128 to 127(Can we change the range?). So the reference variables i11(10) and i12(10) in the above example refer to the same Integer Object. But for the value 1000 two different Objects are created.

Thanks.

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

No, you cannot change this limit. And I dont see any reason that any developer will ever need to do that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

rohan yadav wrote:Can't believe you have done SCJP and SCWCD

Be Nice. Patil is SCJP 1.4 where this wasn't part of Java yet. SCWCD also does not include any information about auto(un)boxing. It is perfectly possible to get SCJP 1.4 and SCWCD 5 without any knowledge of auto(un)boxing, or any of the other features added in Java 5.0.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper comparison.