Saturday, 23 April 2016

New features of Java 1.7 (Java 7)

1. switch on String
Before Java 1.7, only integral types (including char) can be used as selector for switch-case statement.
In the Java 1.7, We can use a String object in the expression of a switch statement.

Example
String day = "SAT";
switch (day) {
   case "MON": System.out.println("Monday");    break;
   case "TUE": System.out.println("Tuesday");   break;
   case "WED": System.out.println("Wednesday");   break;
   case "THU": System.out.println("Thursday");   break;
   case "FRI": System.out.println("Friday");   break;
   case "SAT": System.out.println("Saturday");   break;
   case "SUN": System.out.println("Sunday");    break;
   default:  System.out.println("Invalid");
}


2. Binary Literals (with prefix "0b")
Before Java 1.7, you can only use octal values (with prefix '0') or hexadecimal values (with prefix '0x' or '0X').
In Java 1.7, you can define literal values in binary with prefix '0b' (or '0B') for integral types (byte, short, int and long)

Example
byte byteVar = (byte) 0b00100100;
short shortVar = (short) 0b1010101101001101;
int intVar = 0b10100001010001011011000101101101;
long longVar = 0b1010000101000101101000010100010110101011010101011010101101000101L;


3. Underscores in Numeric Literals
In Java 1.7, it is permitted to use underscore (_) to break the digits to improve the readability but you must start and end with a digit.

Example
long creditCardNumber = 1234_5678_9012_3456L;
float pi = 3.14_15F;
byte aByte = (byte) 0b0110_1101;
long hexBytes = 0xFF_EC_DE_5E;

Rules
You cannot place underscores in the following places :
  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Prior to an F or L suffix

4. Catching multiple exceptions
In Java 1.7, a single catch block can handle more than one type of exception. (Reduces code duplication)
Prior to Java 1.7, it is difficult to create a common method to eliminate the duplicated code because the exception variable in catch has different types.

Example
You can use one single catch block, with exception types separated by '|'
try {
   ......
} catch(ClassNotFoundException|SQLException ex) {
   logger.log(ex); 
   ex.printStackTrace();
}


5. try-with-resource Statement
Before Java 1.7, we need to use a finally block, to ensure that a resource is closed.
try-with-resources statement is a try statement that declares one or more resources and ensures that each resource is closed at the end of the statement.
Any object that implements java.lang.AutoCloseable (which includes all objects which implement java.io.Closeable) can be used as a resource.

Example (before Java 1.7)
try {
  BufferedReader in  = new BufferedReader(new FileReader("in.txt"));
  BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) {
  int charRead;
  while ((charRead = in.read()) != -1) {
    System.out.printf("%c ", (char)charRead);
    out.write(charRead);
  }
} catch (IOException ex) {
  ex.printStackTrace();
} finally {
  if (in != null) in.close();
  if (out != null) out.close();
}

Example (In Java 1.7)  finally block is not required to close the resources - in and out
try (BufferedReader in  = new BufferedReader(new FileReader("in.txt"));
       BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) {
         int charRead;
         while ((charRead = in.read()) != -1) {
            System.out.printf("%c ", (char)charRead);
            out.write(charRead);
         }
} catch (IOException ex) {
        ex.printStackTrace();
}

Example-2
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  return br.readLine();
} catch (Exception e) {
  e.printStackTrace();
}


6. Type inference for Generic instance creation
You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (also called a "Diamond")

Example (before Java 1.7)
Map<String, List<String>> myMap = new HashMap<String, List<String>>();

Example (Java 1.7)
In Java 1.7, you can substitute the parameterized type of the constructor with an empty set of type parameters :
Map<String, List<String>> myMap = new HashMap<>();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.