autoboxing and unboxing in java

autoboxing and unboxing in java –


autoboxing and unboxing concepts is related to primitive data types and its wrapper classes. If any wrapper class holds the value of its primitive data type called boxing. And if any primitive data type holds the objects of its wrapper class called unboxing.

autoboxing and unboxing in java is two types :
1) Implicit Auto boxing.
2) Explicit boxing.
3) Implicit unboxing.
4) Explicit unboxing.


Implicit Auto boxing –


J2SE 5 was added autoboxing. Autoboxing is internal process where primitive data type automatically encapsulated within their wrapper classes. So there is no need to wrap primitive data type to its wrapper classes. There is really very helpful with in collection framework. Because collections framework allows objects instead of primitive data type.

Example :-

Integer intObj = 786; // Autoboxing
List listObj = new ArrayList();
listObj.add(786); // Autoboxing
listObj.add(787); // Autoboxing

So in this line there is no need to make an object and then explicitly encapsulate the value.


Explicit boxing –


Before J2SE 5, autoboxing was not present then developers was use explicit boxing.

Example –
List listObj = new ArrayList();
listObj.add(new Integer(786)); // Explicit boxing
listObj.add(new Integer(787)); // Explicit boxing


Implicit unboxing (Auto-unboxing) –


J2SE 5 was added unboxing. unboxing is internal process where wrapper classes objects directly converted to primitive data type automatically. So there is no need to convert manually to primitive data types.

Example –
Integer intObj = 786; // Autoboxing
Int intvariable = intObj;
//or
List listObj = new ArrayList();
listObj.add(new Integer(786)); // Explicit boxing
listObj.add(new Integer(787)); // Explicit boxing
int x = listObj.get(0);   //Implicit unboxing
int y = listObj.get(1);   //Implicit unboxing


Explicit unboxing –


Before J2SE 5 , developers was do explicitly unboxing.

Example –
Integer intObj = 786; // Autoboxing
Int intvariable = intObj.intValue();   // Explicit unboxing
//or
List listObj = new ArrayList();
listObj.add(new Integer(786));      // Explicit boxing
listObj.add(new Integer(787));      // Explicit boxing
int x = new Integer(listObj.get(0)).intValue(); // Explicit unboxing
int y = new Integer(listObj.get(1)).intValue(); // Explicit unboxing

Comments

  1. Hi,
    Autoboxing is a great feature provided by JAVA5 but using it blindly may result in very subtle problem which will take hours and hours to
    debug and find. to read more see the link
    What is the problem while using '==' in autoboxing world in Java 5 ?

    Thanks
    Javin

    ReplyDelete
  2. Thanks for such a nice blog, Big Rock Designs. I am aware of many link building processes. Rest, I am aware of more from here and all of them are very nice and contains awesome info. Actually, I am pursuing digital marketing course right now and hence looking for such type of blogs to gain more information associated with On-Page SEO.
    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    ReplyDelete
  3. Great man .Hats off for ur simple demonstration.Until now i was not clear in this concept.But now im 100 satisfied and come to know about autoboxing and unboxing.Keep blogging.
    apple service center near me
    iphone service
    iphone service centres in chennai
    iphone service center velachery
    iphone service center in vadapalani
    iphone service center in porur


    ReplyDelete

Post a Comment

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

Shallow Copy Deep Copy Java example