Google

Apr 11, 2014

Top 8 new features in Java 7 with examples

There are several small new features and enhancements in Java 7. The major features and enhancements coming in Java 8. Let's look at the Java 7 new features.

#1: string in switch statement:

public class Java7Feature1 {

 private static String color = "BLUE";

 private enum Color {
  RED, GREEN
 };

 public static void main(String[] args) {

  // Pre Java 5
  if (color.equals("RED")) {
   System.out.println("Color is Red");
  } else if (color.equals("GREEN")) {
   System.out.println("Color is Green");
  } else {
   System.out.println("Color not found");
  }

  // Java 5 enum. try/catch is required for colours other than RED and GREEN 
  try {
   switch (Color.valueOf(color)) {
   case RED:
    System.out.println("Color is Red");
    break;
   case GREEN:
    System.out.println("Color is Green");
   }
  } catch (IllegalArgumentException e) {
   System.out.println("Color not found");
  }

  // Java 7 String in switch statement for simplicity & better readability
  //JDK 7 switch performs better than if-else
  //using types with enums is only useful when it serves a meaningful purpose
  //the value for color could come from database, and string in switch is handy for this
  switch (color) {
  case "RED":
   System.out.println("Color is Red");
   break;
  case "GREEN":
   System.out.println("Color is Green");
   break;
  default:
   System.out.println("Color not found");
  }
 }

}



Output is:

Color not found
Color not found
Color not found


#2 Binary integral literals

public class Java7Feature2 {

 public static void main(String[] args) {
  // Pre Java 7
  int n = Integer.parseInt("10000000", 2);
  System.out.println(n);

  n = 1 << 7;
  System.out.println(n);

  // Java 7
  n = 0b10000000; // 128 = 2^7
  System.out.println(n);
 }
}


Output:

128
128
128


#3: Underscores for better readability in numeric literals

public class Java7Feature3 {

 public static void main(String[] args) {
         //pre Java 7
   int million = 1000000;
   System.out.println(million);
   
   //Java 7. More readable
   million = 1_000_000;
   System.out.println(million);
      
   //consecutive underscores are allowed
   int ten_million = 10__000_000;
   System.out.println(ten_million);
      
   //underscores can be used in other numeric types
   double million_dollars_5_cents = 1_000_000.0_5d;
   System.out.println(million_dollars_5_cents);
   
   //illegal to have underscores 
   //1. start or end a literal with an underscore _10.00, 10.00_
   //2. have underscores before or after a decimal point 10_.00, 10._00
 }
}

Output:

1000000
1000000
10000000
1000000.05



#4: AutoCloseable interface. 

Java 5 introduced the Closeable interface and Java 7 has introduced the AutoCloseable interface to avoid the unsightly try/catch/finally(within finally try/catch) blocks to close a resource. It also prevents potential resource leaks due to not properly closing a resource. The java.io.InputStream and java.io.OutputStream now implements the AutoCloseable interface.



try-with-resources is one of the most useful additions in Java 7.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Java7Feature4 {

 public static void main(String[] args) {
  // pre Java 7
  BufferedReader br = null;

  try {
   File f = new File("c://temp/simple.txt");
   InputStream is = new FileInputStream(f);
   InputStreamReader isr = new InputStreamReader(is);
   br = new BufferedReader(isr);

   String read;

   while ((read = br.readLine()) != null) {
    System.out.println(read);
   }
  } catch (IOException ioe) {
   ioe.printStackTrace();
  } finally {
   try {
    if (br != null)
     br.close();
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }

  
  // Java 7 -- more concise 11 lines as opposed to 20 lines
  try (InputStream is = new FileInputStream(new File("c://temp/simple.txt"));
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br2 = new BufferedReader(isr);) {

   String read;

   while ((read = br2.readLine()) != null) {
    System.out.println(read);
   }

  }
  catch (IOException ioe) {
   ioe.printStackTrace();
  }

 }

}

The output is:

Big
brown fox
jumped over the fence
Big
brown fox
jumped over the fence


try can now have multiple statements in the parenthesis and each statement should create an object which implements the new java.lang.AutoCloseable interface. The AutoCloseable interface consists of just one method. void close () throws Exception {}. Each AutoClosable resource created in the try statement will be automatically closed! If an exception is thrown in the try block and another Exception is thrown while closing the resource, the first Exception is the one eventually thrown to the caller.

Think of the close( ) method as implicitly being called as the last line in the try block.


Features 5 to 8 are covered in Java 8 new features in Java 7 with examples -- part 2

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home