Tuesday, 31 March 2015

Autoboxing and Unboxing in Java

Autoboxing and Unboxing:

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code.

Advantage of Autoboxing and Unboxing:

No need of conversion between primitives and Wrappers manually so less coding is required.

Simple Example of Autoboxing in java:

1.        
2.    class BoxingExample1{  
3.      public static void main(String args[]){  
4.        int a=50;  
5.            Integer a2=new Integer(a);//Boxing  
6.      
7.            Integer a3=5;//Boxing  
8.              
9.            System.out.println(a2+" "+a3);  
10.  }   
11. }  
      
Output:50 5

Simple Example of Unboxing in java:

The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let's see the example of unboxing:
1.        
2.    class UnboxingExample1{  
3.      public static void main(String args[]){  
4.        Integer i=new Integer(50);  
5.            int a=i;  
6.              
7.            System.out.println(a);  
8.     }   
9.    }  
      
Output:50

Autoboxing and Unboxing with comparison operators

Autoboxing can be performed with comparison operators. Let's see the example of boxing with comparison operator:
1.        
2.    class UnboxingExample2{  
3.      public static void main(String args[]){  
4.        Integer i=new Integer(50);  
5.              
6.            if(i<100){            //unboxing internally  
7.            System.out.println(i);  
8.            }  
9.     }   
10. }  
   

Output:50

Autoboxing and Unboxing with method overloading

In method overloading, boxing and unboxing can be performed. There are some rules for method overloading with boxing:
·         Widening beats boxing
·         Widening beats varargs
·         Boxing beats varargs

1) Example of Autoboxing where widening beats boxing

If there is possibility of widening and boxing, widening beats boxing.
1.        
2.    class Boxing1{  
3.      static void m(int i){System.out.println("int");}  
4.      static void m(Integer i){System.out.println("Integer");}  
5.      
6.      public static void main(String args[]){  
7.       short s=30;  
8.       m(s);  
9.     }   
10. }  

      
Output:int

2) Example of Autoboxing where widening beats varargs

If there is possibility of widening and varargs, widening beats var-args.
1.        
2.    class Boxing2{  
3.      static void m(int i, int i2){System.out.println("int int");}  
4.      static void m(Integer... i){System.out.println("Integer...");}  
5.      
6.      public static void main(String args[]){  
7.       short s1=30,s2=40;  
8.       m(s1,s2);  
9.     }   
10. }  

Output:int int

3) Example of Autoboxing where boxing beats varargs

Let's see the program where boxing beats variable argument:
1.        
2.    class Boxing3{  
3.      static void m(Integer i){System.out.println("Integer");}  
4.      static void m(Integer... i){System.out.println("Integer...");}  
5.      
6.      public static void main(String args[]){  
7.       int a=30;  
8.       m(a);  
9.     }   
10. }  

Output:Integer

Method overloading with Widening and Boxing

Widening and Boxing can't be performed as given below:
1.        
2.    class Boxing4{  
3.      static void m(Long l){System.out.println("Long");}  
4.      
5.      public static void main(String args[]){  
6.       int a=30;  
7.       m(a);  
8.     }   
9.    }  
      

Output:Compile Time Error


Static Import in Java

Static Import:

The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.

Advantage of static import:

  • Less coding is required if you have access any static member of a class oftenly.

Disadvantage of static import:

  • If you overuse the static import feature, it makes the program unreadable and unmaintainable.

Simple Example of static import

1.    import static java.lang.System.*;    
2.    class StaticImportExample{  
3.      public static void main(String args[]){  
4.         
5.       out.println("Hello");//Now no need of System.out  
6.       out.println("Java");  
7.      
8.     }   
9.    }  


Output:Hello
       Java
  

What is the difference between import and static import?


The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.

Var-args in Java

Variable Argument (Varargs):

The varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem. If we don't know how many argument we will have to pass in the method, varargs is the better approach.

Advantage of Varargs:

We don't have to provide overloaded methods so less code.

Syntax of varargs:

The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:
1.    return_type method_name(data_type... variableName){}  

Simple Example of Varargs in java:

1.        
2.    class VarargsExample1{  
3.       
4.     static void display(String... values){  
5.      System.out.println("display method invoked ");  
6.     }  
7.      
8.     public static void main(String args[]){  
9.      
10.  display();//zero argument   
11.  display("my","name","is","varargs");//four arguments  
12.  }   
13. }  
      
Test it Now
Output:display method invoked
       display method invoked

Another Program of Varargs in java:

1.        
2.    class VarargsExample2{  
3.       
4.     static void display(String... values){  
5.      System.out.println("display method invoked ");  
6.      for(String s:values){  
7.       System.out.println(s);  
8.      }  
9.     }  
10.   
11.  public static void main(String args[]){  
12.   
13.  display();//zero argument   
14.  display("hello");//one argument   
15.  display("my","name","is","varargs");//four arguments  
16.  }   
17. }  

     
Test it Now
Output:display method invoked
       display method invoked
       hello
       display method invoked
       my
       name
       is 
       varargs


Rules for varargs:

While using the varargs, you must follow some rules otherwise program code won't compile. The rules are as follows:
  • There can be only one variable argument in the method.
  • Variable argument (varargs) must be the last argument.

Examples of varargs that fails to compile:

1.        
2.    void method(String... a, int... b){}//Compile time error  
3.      
4.    void method(int... a, String b){}//Compile time error  
5.          

Example of Varargs that is the last argument in the method:

1.        
2.    class VarargsExample3{  
3.       
4.     static void display(int num, String... values){  
5.      System.out.println("number is "+num);  
6.      for(String s:values){  
7.       System.out.println(s);  
8.      }  
9.     }  
10.   
11.  public static void main(String args[]){  
12.   
13.  display(500,"hello");//one argument   
14.  display(1000,"my","name","is","varargs");//four arguments  
15.  }   
16. }  
17.  
Test it Now
Output:number is 500
       hello
       number is 1000
       my
       name
       is 

       varargs

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...