Sunday 14 May 2017

Java 7 Features

This post covers important features of Java 7. There was good number of changes introduced in Java 7. In this post I'm going to list out most important and frequently used features.


1. Strings in switch case 

Prior to Java 7 strings was not allowed in switch case. You should use If-Else condition instead as an alternative.

Before Java 7:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class StringInSwitch {
 public static void main(String[] args) {
  String choice = "Java 7";
  
  if("Java 7".equalsIgnoreCase(choice))
   System.out.println("Cool!!");
  if("Java 6".equalsIgnoreCase(choice))
   System.out.println("Not Cool!!");
  if("Java 8".equalsIgnoreCase(choice))
   System.out.println("Super Cool!!");
 }
}

After Java 7:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class StringInSwitch {
 public static void main(String[] args) {
  String choice = "Java 7";
  switch(choice){
  case "Java 7":
   System.out.println("Cool!!");
   break;
  case "Java 6":
   System.out.println("Not Cool!!");
   break;
  case "Java 8":
   System.out.println("Super Cool!!");
   break;
  default:
   break;
  }
 }
}


2. Handling multiple exception in one catch block

Before Java 7 you need to write separate catch block for each and every exception. But form Java 7 on wards you can club multiple exceptions in one single block. This features makes the code manageable and readable.

Before Java 7:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class HandlingMultipleException {
 public static void main(String[] args) {
  FileInputStream fi;
  try {
   fi = new FileInputStream(new File("Test.txt"));
   Thread.sleep(1000);
  } catch (IOException e) {
   e.printStackTrace();
  }catch(InterruptedException e){
   e.printStackTrace();
  }
  
 }
}

After Java 7:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class HandlingMultipleException {
 public static void main(String[] args) {
  FileInputStream fi;
  try {
   fi = new FileInputStream(new File("Test.txt"));
   Thread.sleep(1000);
  } catch (IOException | InterruptedException e) {
   e.printStackTrace();
  }
  
 }
}

3. try with resource

This is one of the most useful feature which help you in resource management. Before Java 7 It was programmer's responsibility to close all the resources(Files, Input Stream, Connection etc.) inside finally block in order avoid resource leakage. This manual approach was error prone because most of time programmers forget to close the resources. From Java 7 on wards we can use try with resource where we can initialize all our resources inside try block, thereafter Java it self manages those resource. Java it self closes all the resources when they are no more required in your code. 

Before Java 7:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TryWithResource {
 public static void main(String[] args) {
  BufferedReader br = null;
  FileInputStream fi = null;
  try {
   fi = new FileInputStream(new File("Test.txt"));
   br = new BufferedReader(new InputStreamReader(fi));
   br.readLine();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    br.close();
    fi.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
 }
}

After Java 7: 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class TryWithResource {
 public static void main(String[] args) {
  try(FileInputStream fi = new FileInputStream(new File("Test.txt"));
   BufferedReader br = new BufferedReader(new InputStreamReader(fi));) {
   br.readLine();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

4. Automatic type Inference(Diamond operator)

Before Java 7:
1
2
3
4
5
6
public class TypeInference {
 public static void main(String[] args) {
  List<String> list = new ArrayList<String>();
  Map<Integer, String> map = new HashMap<Integer, String>();
 }
}

After Java 7:
1
2
3
4
5
6
public class TypeInference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		Map<Integer, String> map = new HashMap<>();
	}
}

No comments:

Post a Comment