Java platform, standard Edition 7 has released major features. In this article we will learn all these features with examples. In all software companies fallowing these Java 7 features to develop their software projects.Then we late,let us discuss what they are?
The key features introduced as part of Java 7 programming:
The key features introduced as part of Java 7 programming:
- Binary Literals
- Strings in Switch Statements
- Type interface for Generic instance Creation
- Improved Compiler Warnings and Errors when using Non-Reliable formal parameters with Varagrs methods
- The try-with-resources statement
- Catching Multiple Exception types and Rethrowing Exceptions with improved types checking
- Multi Language Virtual Machine
- Garbage First
Binary Literals:
In java 7, the integral types such as byte,short,int and long can also be expressed using the binary number system. To represent a binary literal add the prefix 0b or 0B to the number.
The main purpose of this features is can make relationship among data.
Example:
//An 8-bit 'byte' value:
byte aByte=(byte)0b00100001;
//A 16-bit 'short-value":
short aShort=(short)0b1010000101000101;
//some 32-bit 'int' value:
int anInt1=0b10100001010001011010000101000101;
int anInt2=0b101;
int anInt3=0B101;//the B can be upper or lower case.
Strings in Switch Statement:
Before java 7, only integral types can be used as selector for switch-case statement. In java 7,you can use a String object as the selector,equals() and hashcode()method from java.lang.String is used in comparison,which is case sensitive. The main purpose of using String in switch is that,java compiler can generate more efficient code than using nested if-then-else statement
Example:
String s=...;
Switch(s)
{
case "pavan":
System.out.println("Hi i am pavan");
break;
case "mahi":
System.out.println("Hi i am mahi");
break;
case "lucky":
System.out.println("Hi i am lucky");
break;
default :
throw new IllegalArgumentException("Invalid data"+s);
}
Type Interface:
You can replace the type arguments required to invoke constructor of a generic class with an empty set of type parameters(<>) as long as the compiler can infer the type arguments from the context.This pair of angle brackets is called the diamond.
Prior java 7, you type more to specify types on both left and right hand side of object creation expression,but now it only needed on left hand side.
Example:
In previous we need to write like below:
Map <String,Integer>myMap=new HashMap<String,Integer>();
But in java 7, we can write like below
Map<String,Integer>myMap=new HashMap<>();
Non-Reifiable types:
A non-reifiable type is a type that is not completely available at run time. At compile time,non-reifiable types undergo a process called type erasure during which the compiler removes information related to type parameters and type arguments. This ensures binary compatibility with java libraries and applications that were created before generics. Because type erasure removes information from parameterized types at compile-time these types are non-reifiable.
Try-with-resources statement:
This is one of the key feature which cuts line of code and also make the code robust. Before java 7,we had to use finally blocks to cleanup the resources. Finally blocks were not mandatory,but resource clean up was to prevent the system from being corrupt. With java 7, there is no need to explicit resource clean up. It is done automatically. Automatic resource cleanup is done when initializing resource in try-with-resource block try(..){...}).
Example:
Previous:
BufferedReader br=new BufferedReader(new FileReader(path));
try{
return br.readLine();
}
finally{
br.close();
}
In java 7 Like as fallows:
try(BufferedReader br=new BufferedReader(new FileReader(path))
{
return br.readLine();
}
Also Read: File in java with examples
How to catch Multiple exception types:
In java 7 allows you to 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
Example:
Before:
}
catch(FirstException e1)
{
System.out.println(e1);
throw e1;
}
catch(SecondException e2)
{
System.out.println(e2);
throw e1;
}
NOW:
}
catch(FirstException|SecondException e)
catch(FirstException|SecondException e)
{
System.out.println(e);
throw e;
}
Multi language Virtual machine:
Java 6 provided the capability to plug-in other languages in the VM. Java 7 to be strengthened to make other languages run even better in the JVM. In java 7, JVM to be optimized to make the language specific feature run better.
Example: Closures
Garbage First:
In java 7 new Garbage collector introduced. It is called Garbage First Collector. Memory split into multiple regions as opposed to 2 regions in the current version. It performs faster than current parallel collector.
No comments:
Post a Comment