• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Wrappers & Primitives Comparision

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?...
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
please buy my thing and then I'll have more money:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic