Followers

coreJava : Features of java

  • Strings in switch Statements(java 7)
  • For-each loop (Advanced or Enhanced For loop)(Java 5):
  • Variable Argument (Varargs) (Java 5):
  • Java Static Import (Java 5)
  • Autoboxing and Unboxing (Java 5):



1.Strings in switch Statements(java 7)

In the JDK 7 release, you can use a String object in the expression of a switch statement:

package com.switch1;
public class Switch1 {
 public static String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
      String typeOfDay;
      switch (dayOfWeekArg) {
          case "Monday":
              typeOfDay = "Start of work week";
              break;
          case "Tuesday":
          case "Wednesday":
          case "Thursday":
              typeOfDay = "Midweek";
              break;
          case "Friday":
              typeOfDay = "End of work week";
              break;
          case "Saturday":
          case "Sunday":
              typeOfDay = "Weekend";
              break;
          default:
              throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
      }
      return typeOfDay;
 }

 public static void main(String[] args) {
  System.out.println(getTypeOfDayWithSwitchStatement("Sunday"));

 }

}



2.For-each loop (Advanced or Enhanced For loop)(Java 5):

It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
Advantage of for-each loop:
          It makes the code more readable.
          It eliminates the possibility of programming errors.
Syntax of for-each loop:
       for(data_type variable : array | collection){} 

Simple Example of for-each loop for traversing the array elements:
   
class ForEachExample1{ 
  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("vimal"); 
   list.add("sonoo"); 
   list.add("ratan"); 
 
   for(String s:list){ 
     System.out.println(s); 
   } 
 
 }  
   

Output:vimal
       sonoo
       ratan

3.Variable Argument (Varargs) (Java 5):

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 Test{ 
  
 static void display(String... values){ 
  System.out.println("display method invoked "); 
 } 
 
 public static void main(String args[]){ 
 
 display();//zero argument  
 display("my","name","is","varargs");//four arguments 
 }  

Output:display method invoked
       display method invoked

Another Program of Varargs in java:   
class Test2{ 
  
 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","varargs");//four arguments 
 }  

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:
   
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 Test3{ 
  
 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","varargs");//four arguments 
 }  

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

4.Java Static Import (Java 5)

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
import static java.lang.System.*;   
class StaticImportExample{ 
  public static void main(String args[]){ 
    
   out.println("Hello");//Now no need of System.out 
   out.println("Java"); 
 
 }  
}  

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.

5.Autoboxing and Unboxing (Java 5):

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:
   
class BoxingExample1{ 
  public static void main(String args[]){ 
    int a=50; 
        Integer a2=new Integer(a);//Boxing 
 
        Integer a3=5;//Boxing 
          
        System.out.println(a2+" "+a3); 
 }  
     
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:
   
class UnboxingExample1{ 
  public static void main(String args[]){ 
    Integer i=new Integer(50); 
        int a=i; 
         
        System.out.println(a); 
 }  
     
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:
   
class UnboxingExample2{ 
  public static void main(String args[]){ 
    Integer i=new Integer(50); 
         
        if(i<100){            //unboxing internally 
        System.out.println(i); 
        } 
 }  
     
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.
   
class Boxing1{ 
  static void m(int i){System.out.println("int");} 
  static void m(Integer i){System.out.println("Integer");} 
 
  public static void main(String args[]){ 
   short s=30; 
   m(s); 
 }  
     
Output:int

2) Example of Autoboxing where widening beats varargs
If there is possibility of widening and varargs, widening beats var-args.
   
class Boxing2{ 
  static void m(int i, int i2){System.out.println("int int");} 
  static void m(Integer... i){System.out.println("Integer...");} 
 
  public static void main(String args[]){ 
   short s1=30,s2=40; 
   m(s1,s2); 
 }  
     
Output:int int

3) Example of Autoboxing where boxing beats varargs
Let's see the program where boxing beats variable argument:
   
class Boxing3{ 
  static void m(Integer i){System.out.println("Integer");} 
  static void m(Integer... i){System.out.println("Integer...");} 
 
  public static void main(String args[]){ 
   int a=30; 
   m(a); 
 }  
     
Output:Integer

Method overloading with Widening and Boxing
Widening and Boxing can't be performed as given below:
   
class Boxing4{ 
  static void m(Long l){System.out.println("Long");} 
 
  public static void main(String args[]){ 
   int a=30; 
   m(a); 
 }  
     
Output:Compile Time Error



No comments:

Post a Comment