• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Autoboxing

October 20, 2011 By j2eereference Leave a Comment

Autoboxing

J2se5 added the new feature called Autoboxing / auto-unboxing.Auto boxing is nothing but converting a primitive type automatically to its corresponding type wrapper whenever its required the type as wrapper. Hence we don’t have to create a wrapper object explicitly.The same way auto-unboxing is creating the primitive type from the wrapper when it is required; and we don’t have to call intVal() or floatValue() methods.

Lets see this with an example :

1
2
3
4
5
6
7
8
class AutoBoxingDemo
{
    public static void main(String args[])
    {
     Integer iObj=25; //Auto boxing
     System.out.println(iObj);
    }
}

Output :

25

 

Here in the above example while we are assigning 25 – primitive type value to the wrapper object iObj ; java convert this primitive to wrapper object automatically.

unboxing

Let’s see another example for for auto-unboxing

1
2
3
4
5
6
7
8
9
class AutoBoxingDemo
{
    public static void main(String args[])
   {
    Integer iObj=25;
    int i=iObj; // Auto unboxing
    System.out.println(i);
   }
}

Output :

25

Here in the above example while we are assigning a wrapper object iObj  to primitive type variable; Java convert the wrapper object iObj to its corresponding primitive value.

Now we have auto boxing and auto-unboxing in from j2se5 onwards. This will lead a performance issue if we are not using it properly. Let’s see one example of bad usage of boxing and unboxing.

1
2
3
4
5
6
7
8
9
10
11
class Demo
{
    public static void main(String args[])
    {
    Integer a,b,c;
    a=25;
    b=50;
    c=(a+b)*2;
    System.out.println(“Result is  ”+c);
    }
}

The above code is correct and it will run successfully. But this has a performance issue. The reason is that each autobox and unbox adds overhead; which is not there if we use primitive type.

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.