Home  |   STIU  |   WOW  |   SCJP  |   SCDJWS   |   JEE FAQ  |   About US  |  

FAQ
  Java FAQ
  JSP FAQ
  Servlet FAQ
 

Advertisement

XyzWs Java FAQ:
What Should I Know about AutoBoxing and UnBoxing in Java 5.0?


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
Advertisement
XyzWs Java FAQ: What Should I Know about AutoBoxing and UnBoxing in Java 5.0?

What Should I Know about AutoBoxing and UnBoxing in Java 5.0?

Within Java 5.0, wrapper classes have become easier to use. Java 5.0 introduced automatic conversion between a primitive type and the corresponding wrapper class.

From primitive type to it corresponse wrapper class is called autoboxing, the reverse process is called unboxing. Autoboxing and unboxing also apply to methods calls. For example, you can pass an argument of type int to a method that has a formal parameter of type Integer.

  • A NullpointerException exception occurs When unboxing an null wrapper class's reference to its primitive type. For example, the code will compile but it will throw a NullpointerException at runtime.
        ...
        Long  L = null;
        long  l = L;
        ...
    
  • Boxing conversion converts values of primitive type to corresponding values of reference type. But the primitive types can not be widened/Narrowed to the Wrapper classes and vice versa. For example,
        byte  b    = 43;
    
        Integer I1 = 23;      //Constant integer value
        Integer I2 = (int)b;  //Cast to int type
    
        Long    L1 = 23;       //compile error because 23 is integer value
        Long    L2 = (Long)23; //can not cast integer value to Long wrapper class
    
        Long    L3 = 23L;
        Long    L4 = (long)23;
    
    

    This restriction is also applied to method invocation:

    public class MyClass  {
        public void method(Long i) {
            System.out.println("Here");
        }
        public static void main(String[] args) {
        
            MyClass s = new MyClasslass();
            //s.method(12);	// error
            s.method(12L);  // ok
        }
    
    }
    
  • When invoking a method from multiple overloading methods, For the matching method process, the Java compiler will perferance the order of primitive types (Widening Primitive Conversion), wrapper class (Boxing Conversion), and var-args. For example,
    public class MyClass  {
        public void method(Long x, Long y) {
            System.out.println("method(Long x, Long y)");
        }
        public void method(long x, long y) {
            System.out.println("method(long x, long y)"); 
        }
        public void method(long... x) {
            System.out.println("method(long... x)");
        }
        public static void main(String[] args) {
            long x, y;
            x = y = 0;
            MyClass s = new MyClass();
            s.method(x, y);	
        }
    }
    

    The result is "method(long x, long y)". The Java compiler will check for the matching primitive types, then it will search for the Wrapper types.

    public class MyClass  {
        public void method(Long x, Long y) {
            System.out.println("method(Long x, Long y)");
        }
        public void method(int x, int y) {
            System.out.println("method(int x, int y)"); 
        }
        public void method(long... x) {
            System.out.println("method(long... x)");
        }
        public static void main(String[] args) {
            long x, y;
            x = y = 0;
            MyClass s = new MyClass();
            s.method(x, y);	
        }
    }
    

    The result is "method(Long x, Long y)". The Java compiler gives preferance to the matching Wrapper class method signature other than the primitive varargs method.

    public class MyClass  {
        public void method(Double x, Double y) {
            System.out.println("method(Double x, (Double y)");
        }
        public void method(int x, int y) {
            System.out.println("method(int x, int y)"); 
        }
        public void method(long... x) {
            System.out.println("method(long... x)");
        }
        public static void main(String[] args) {
            long x, y;
            x = y = 0;
            MyClass s = new MyClass();
            s.method(x, y);	
        }
    }
    

    The result is "method(long ...x)". The compiler will not narrow down "long" primitive value to "int"; Also, it can not winden long to Double class. Only the var-args method can be used.

    public class MyClass  {
        public void method(Long x, Long y) {
            System.out.println("method(Long x, Long y)");
        }
        public static void main(String[] args) {
            int x, y;
            x = y = 0;
            MyClass s = new MyClass();
            s.method(x, y);	
        }
    }
    

    The arguments can not winden to "long" and then box to "Long". You will get compile error.

  • In order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same. Please read Why does the autoboxing conversion sometimes return the same reference?

We had listed few points about "AutoBoxing and UnBoxing" and may not completely, please read CHAPTER 5 Conversions and Promotions and AutoBoxing for more information.


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Support  | Feedback  | Help