This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers & Primitives Comparision

 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This example is from wrappers and primitives comparision

How does the equality between wrappers and primitives evaluated ??
Based on the following quote,

NOTE, certain primitives are always to be boxed into the same immutable wrapper objects. These objects are then CACHED and REUSED, with the expectation that these are commonly used objects. These special values are:

The boolean values true and false.

The byte values.

The short and int values between -128 and 127.

The char values in the range '\u0000' to '\u007F'.

I can understand some of the statements. But, I am confused with many statments, particularly 1, 6, 9.

One more doubt, will autoboxing be applied only at the time of assignments and not at the time of comparisions??

Thanks,
Lalitha.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think this above part is from scjp 1.5 syllyabus ?

pls acknowledge as me preparing for scjp1.4 and not seen == operator b/w wrapper object and primitive...

and what this autoboxing is all about ?...
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalitha's posting says "I am confused with many statments, particularly 1, 6, 9."

Statement 1 concerns this code:
////////////////////////////////////////////////////////////
Integer i1 = new Integer(2);
Integer i2 = new Integer(2);
System.out.println(i1 == i2); // false ..........1
////////////////////////////////////////////////////////////

Here two distinct objects are created, so the references i1 and i2 are different.


Statement 6 concerns this code:
////////////////////////////////////////////////////////////
Integer kkk1 = 128;
int kkk2 = 128;
System.out.println(kkk1 == kkk2); // true ........
////////////////////////////////////////////////////////////

And Statement 9 concerns this:
////////////////////////////////////////////////////////////
int mm1 = -129;
Integer mm2 = -129;
System.out.println(mm1 == mm2); // true ...........9
////////////////////////////////////////////////////////////

The last 2 code chunks are nearly identical. The big difference is that in (6) we have (wrapper == primitive) and in (9) we have (primitive == wrapper). The real question is: does the primitive get boxed (so that a reference is compared to a reference)? Or does the wrapper get unboxed (so that a primitive is compared to a primitive)? And the answer is that the wrapped object gets unboxed.

So in Statement 6, (kkk1 == kkk2) is equivalent to
(kkk1.intValue() == kkk2). And in Statement 9, (mm1 == mm2) is equivalent to (mm1 == mm2.intValue()).

Hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic