Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Autoboxing  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks,

My questions are in code comments.



Thanks,
Hiram
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if this will shed some light on some of your questions...

Integer i2 = new Integer(100);
System.out.println(b2.equals(i2)); //false

Byte b2 = new Byte((byte)100);
System.out.println(b2.equals((byte)100)); //true


Also check equals method logic for Byte here..
 
Sheriff
Posts: 21503
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hiram Nascimento wrote:c = b; // Okay. b is "boxed" and its reference is assigned to c. Right?


Right.

// Does c get "unboxed" or b "boxed"?
print(c == b); // true


Try the following to see which occurs:

// Why false?
print(c.equals(120)); // false


Because 120 is auto boxed into an Integer, and a Byte and an Integer are never equals.

// Is c "unboxed" or the integer literal "boxed"?
print(c == 120); // true


See my above little test code.
 
Hiram Nascimento
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! Much clear now...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys..sorry frnds..i m not cleared with the concept of boxing and unboxing concept yet..what is the concept of equals and ==.??please explain this..thanks in advance
 
Hiram Nascimento
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cchetan,

When I do "if(c == b)", the == is doing a comparison of the references of these vars. The Byte.equals() method returns true if the equals parameter is a "instanceof" Byte. That's how it was implemented.

The autoboxing is related the Wrapper classes of primitive types. Each primitive type in Java (int, byte, long, float, etc) has a corresponding (wrapper) class (Integer, Byte, Long, Float, etc) to be used when an object is expected instead of a primitive value/type.

Before Java 5.0, the process to handle the interaction of primitive types and its wrappers was "manual"... Autoboxing came to make things easier.

you should read more about Wrapper classes and Autoboxing to understand better that process. Not too complex. But some aspects are implementation details you need to know.

Not easy to summarize all this in one post, but read more about Wrapper classes and Autoboxing and tell us your questions. I'm still studying that too.

Hiram
 
Rob Spoor
Sheriff
Posts: 21503
96
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

cchetan jain wrote:hi guys..sorry frnds..i m not cleared with the concept of boxing and unboxing concept yet..what is the concept of equals and ==.??please explain this..thanks in advance


In short:

Use equals if both operands are Objects (wrapper instances).
Use == if at least one is a primitive.
Use the valueOf method combined with equals if one is a primitive and the other is a wrapper instance that may perhaps be null to prevent NullPointerExceptions:
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks hiram for the concern..can you send me a link for the better understanding of wrapper classes..i have read kathy and bates already..but still confused..so please send me link
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!