• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Autoboxing Issue: Integer Wrapper==double primitive

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question related to autoboxing/unboxing that's been bugging me for some time.

Could someone please explain the mechanism behind the foloowing code?

boolean d = (new Integer("1123")==1123d);
System.out.println(d); => TRUE

Cheers,
Horia

p.s.: @Ankit Garg Thanks for the welcome message.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement probably translates like so



Welcome to javaranch. (ha ! now you will have to thank me too )
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When comparing a primitive and wrapper, the wrapper will be unboxed.
 
Horia Constantin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Deepak Bala thanks for the reply, very quick, btw(The thanks for the welcome message is reserved only to the first greeter)

The reason I'm asking this is that I don't find this intuitive and in case of the exam i would have said compilation fails.

Any links regarding this matter? @Harsh Pensi Where did you find that information?

Cheers,
Horia
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The reason I'm asking this is that I don't find this intuitive and in case of the exam i would have said compilation fails.



Yes I know what you mean. More gotchas here if they interest you
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Horia Constantin wrote:

Any links regarding this matter? @Harsh Pensi Where did you find that information?


Read it in the book for scjp6 by Kathy Sierra and Bert Bates.
 
Horia Constantin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsh Pensi wrote:

Horia Constantin wrote:

Any links regarding this matter? @Harsh Pensi Where did you find that information?


Read it in the book for scjp6 by Kathy Sierra and Bert Bates.



I'am also reading the book. In found the information regarding Wrappers at chapter3, but nothing relevant to my issue.

Could you please specify exactly at what page you found the information.

Cheers,
Horia
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Horia, its on pg. 246.
 
Horia Constantin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsh Pensi wrote:Hi Horia, its on pg. 246.




It's just two wrapper objects that happen to have the same value. Because they
have the same int value, the equals() method considers them to be "meaningfully
equivalent", and therefore returns true. How about this one:
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
This example produces the output:
same object
meaningfully equal
Yikes! The equals() method seems to be working, but what happened with ==
and != ? Why is != telling us that i1 and i2 are different objects, when == is saying
that i3 and i4 are the same object? In order to save memory, two instances of the
following wrapper objects (created through boxing), will always be == when their
primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127
Note: When == is used to compare a primitive to a wrapper, the wrapper will be
unwrapped and the comparison will be primitive to primitive.
Where Boxing Can Be used
As we discussed earlier, it's very common to use wrappers in conjunction with
collections. Any time you want your collection to hold objects and primitives, you'll
want to use wrappers to make those primitives collection-compatible. The general
rule is that boxing and unboxing work wherever you can normally use a primitive or
a wrapped object. The following code demonstrates some legal ways to use boxing:
class UseBoxing {
public static void main(String [] args) {
UseBoxing yu = new UseBoxing();
yu.go(5);
}
boolean go(Integer i) { // boxes the int it was passed
Boolean ifSo = true; // boxes the literal
Short s = 300; // boxes the primitive



This is page 246, i cannot find the relevant paragraph. Could you mark it?
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha i dont believe this.
Did you read the page???
 
Horia Constantin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsh Pensi wrote:Haha i dont believe this.
Did you read the page???



3 times actually...

LE: 4 times

After Ankit Garg posted the response I'm a little ashamed of the fact that I missed that line so many times.
Oh well...

Cheers (and big thanks),
Horia
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

primitive values are the same:
n Boolean
n Byte
n Character from \u0000 to \u007f (7f is 127 in decimal)
n Short and Integer from -128 to 127
Note: When == is used to compare a primitive to a wrapper, the wrapper will be
unwrapped and the comparison will be primitive to primitive.

Where Boxing Can Be used

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Horia, I'm still amused
Anyways, take care with the rest of your preparation
Cheers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic