New Features in Java 7 with examples

1. Diamond Syntax <> for type inference


Diamond operator <> is a new Java 7 feature which provides type inference while creating object of Generic classes.


Prior JDK 7

List<Integer> empList = new ArrayList<Integer>();
Set<String> empSet =  new HashSet<String>();
Map<String, List<String>> students =  new HashMap<String, List<String>>();

In JDK 7

List<Integer> empList = new ArrayList<>();
Set<String> empSet =  new HashSet<>();
Map<String, List<String>> students =  new HashMap<>();


2. Strings in Switch Statements


You can use a String object in a switch statement expression. Prior JDK 7, only integral types can be used as selector for switch-case statement. In JDK 7, you can use a String object as the selector. For Example-


Prior JDK7:

if (language.equals("Java")) {
	System.out.println("Java");
} else if (language.equals("C++")) {
	System.out.println("C++");
} else if (language.equals("PHP")) {
	System.out.println("PHP");
} else if (language.equals("Groovy")) {
	System.out.println("Groovy");
}

In JDK7:

switch (language) {
	case "Java":
		System.out.println("Java");
		break;
	case "C++":
		System.out.println("C++");
		break;
	case "PHP":
		System.out.println("PHP");
		break;
	case "Groovy":
		System.out.println("Groovy");
		break;
}

3. Binary Literals


Starting Java7, all the number types (byte, short, int, long) can be expressed using the binary number system. In order to do same, add the prefix 0b or 0B to the number as shown in following examples. This could be helpful in getting better visibility when doing the bit level operations:

//byte value (8-bit) expressed in binary number system
byte numByte = (byte) 0b10010001;

//short value (16-bit) expressed in binary number system
short numShort = (short) 0b1001000100011110;

//int value (32-bit) expressed in binary number system
int numInt =  0b10010001000111101010101001010101;

//long value (64-bit) expressed in binary number system
long numLong =  0b1001000100011110101010100101010110010001000111101010101001010101L;



4. Catching Multiple Exception Type in Single Catch Block


In this feature, now you can catch multiple exceptions in single catch block. Before java 7, you was restricted to catch only one. To specify the list of expected exceptions a pipe ('|') character is used.


Prior JDK7:

try
{
   //Do some processing which throws Exception
}catch(NullPointerException ex1| IndexOutOfBoundsException ex)
{
       throw ex1;
}
catch(IndexOutOfBoundsException ex2)
{
       throw ex2;
}

In JDK7 :

try
{
   //Do some processing which throws Exception
}
//You can catch multiple exception added after 'pipe' character
catch(NullPointerException | IndexOutOfBoundsException ex)
{
       throw ex;
}


5. The try-with-resources Statement


One of the Java 7 features is try-with-resources statement for automatic resource management. A resource is an object that must be closed once your program is done using it, like a File resource or JDBC resource for database connection or a Socket connection resource. Before Java 7, there was no auto resource management and we should explicitly close the resource once our work is done with it.


Prior JDK7 :

try{
    //open resources like File, Database connection, Sockets etc
} catch (FileNotFoundException e) {
    // Exception handling like FileNotFoundException, IOException etc
}finally{
    // close resources
}

In JDK7 :

try(// open resources here){
    // use resources
} catch (FileNotFoundException e) {
    // exception handling
}


6. G1 Garbage Collector


JDK 7 introduced a new Garbage Collector known as G1 Garbage Collection, which is short form of garbage first. G1 garbage collector performs clean-up where there is most garbage. To achieve this it split Java heap memory into multiple regions as opposed to 3 regions in the prior to Java 7 version (new, old and permgen space). It's said that G1 is quite predictable and provides greater through put for memory intensive applications.













Related Examples


Write a Comparable to compare students based upon their age?
Simple Array Sorting Program
Write a program to convert from decimal to binary?
Write a program to get list of all the files from a given directory?
Producer Consumer Example in Thread
Write a program to reverse a String?
Write a Comparator to compare students based upon their name and age?









Receive Latest Java Examples in Your Email: