• 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 help

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am always confused regarding Autoboxing question. I tried at sun site for help. But Im unable to find anything useful. Can any body suggest me a link where I can find a comprehensive help about Autoboxing, please.

Thanks for any help.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by raja kanak:
I am always confused regarding Autoboxing question...


It would help if you could tell us exactly what you are confused about. I don't know whether you're looking for a summary article (Autoboxing) or detailed specifications (JLS - 5.1.7 and 5.1.8 Boxing and Unboxing Conversions).
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autoboxing:
The way I comprehended!
Hi rajakanak,

Simply understand that wrapper classes incapsulate the corresponding primitive data types.
You can think about a wrapper class like this:

final class Integer extends Object implements Comparable {
int a;
}

Class Integer has encapsulated the primitive type a for example, you can think so;

Using prior Java 5.0 version we are not able to do like

Integer i1 = 5;//error, prior to Java 5.0

Primitive can�t be assigned to object reference variable;

Whereas the correct approach is
Integer i1 = Integer.valueOf(5);//takes int and returns wrapped Integer object
Or
Integer i2 = Integer.valueOf(�5�);//takes String and returns wrapped Integer object

The valueof() is the static method of the Wrapper classes. That take corresponding primitive or String and returns the boxed object(encapsulating primitive) that we assigned to wrapper reference variable i1.

You can feel the trouble of converting a primitive to corresponding wrapper class object. But with Java 5; this happens automatically.

The primitive is automatically boxed to corresponding wrapper class object and assigned to the reference variable in our case to i2; See this:

Interger i2 = 12;//NO problem, autoboxing is done automatically

This is equivalent to
Integer i2= Integer.valueOf(12);

Finally what Boxing means: To box a primitive to the corresponding Wrapper class object. Unboxing means converting the boxed primitive object to the corresponding primitive type. And autoboxing refers to both the activity that is happening automatically without any explicit effort by the programmer. Now see these couple lines of code:


int a = new Integer(12);//unboxing takes place
This is equivalent to
int a = new Integer(12).intValue();

float b = new Float(12.33d);// unboixng takes place,
this is equivalent to
flaot b = new Float(12.33d).intValue();


I hope this helps you well.

With regards,
cmbhatt
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your efforts marc & chandra.
reply
    Bookmark Topic Watch Topic
  • New Topic