Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper comparison.

 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question about == and wrapper objects is a frequently asked question.

See for example this topic for an explanation.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Patil Niteen
Ranch Hand
Posts: 48
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot change this limit. And I dont see any reason that any developer will ever need to do that.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic