Followers

coreJava : Features of java 5.

Below are the main features in java 5.

for-each loop
varargs
static Import
autoboxing and unboxing
generics
annotation
enum Types

For-each loop (Advanced or Enhanced For loop):

It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs , makes the code more readable and no need find the length.

Syntax of for-each loop:
       for(data_type variable : array | collection){} 

Simple Example of for-each loop for traversing the array elements:
   
class ForEachEx1{ 
  public static void main(String args[]){ 
   int arr[]={12,13,14,44};    
   for(int i:arr){ 
     System.out.println(i); 
   } 
 
 }  

Output:
       12
       13
       14
       44


Simple Example of for-each loop for traversing the collection elements:
import java.util.*; 
class ForEachExample2{ 
  public static void main(String args[]){ 
   ArrayList<String> list=new ArrayList<String>(); 
   list.add("Sachin"); 
   list.add("Mohan"); 
   list.add("Tata"); 
 
   for(String s:list){ 
     System.out.println(s); 
   }  
 }  
}          

Output:
      Sachin
      Mohan
      Tata

Variable Argument (varargs):

The varrags allows the method to accept zero or multiple 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:
return_type method_name(data_type... variableName){} 

Simple Example of Varargs in java:
   
class VarargsEx1{     
 static void display(String... values){ 
  System.out.println("display method invoked "); 
 } 
 
 public static void main(String args[]){ 
 
 display();//zero argument  
 display("my","name","is","Mohan");//four arguments 
 }  

Output:
       display method invoked
       display method invoked

Another Program of Varargs in java:
  
class VarargsEx2{     
 static void display(String... values){ 
  System.out.println("display method invoked "); 
  for(String s:values){ 
   System.out.println(s); 
  } 
 } 
 
 public static void main(String args[]){ 
 
 display();//zero argument  
 display("hello");//one argument  
 display("my","name","is","Mohan");//four arguments 
 }  

Output:
       display method invoked
       display method invoked
       hello
       display method invoked
       my
       name
       is
       Mohan

Rules for varargs:

Below are the rules :

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:
   
void method(String... a, int... b){}//Compile time error    
void method(int... a, String b){}//Compile time error 
     
Example of Varargs that is the last argument in the method:
   
class VarargsEx3{     
 static void display(int num, String... values){ 
  System.out.println("number is "+num); 
  for(String s:values){ 
   System.out.println(s); 
  } 
 } 
 
 public static void main(String args[]){ 
 
 display(500,"hello");//one argument  
 display(1000,"my","name","is","Mohan");//four arguments 
 }  
}  

Output:   
       number is 500
       hello
       number is 1000
       my
       name
       is
       Mohan

No comments:

Post a Comment