Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Wrapper comparison.

 
Ranch Hand
Posts: 48
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question about == and wrapper objects is a frequently asked question.

See for example this topic for an explanation.
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ==.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22506
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic