> Java Virtual Support for dynamic languages.
> Java HotSpot Virtual Machine Performance Enhancements.
> Changes in Java Programming Languages:
> Strings in Switch statement.
> Try with Automatic resource Management.
> Enhanced Type Inference for Generic Instance Creation.
> Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods.
> Binary literals.
> Underscores in Numeric Literals.
> Multiple Exception Catching and Exceptions Rethrowing with Improved Type Checking.
> Concurrency Utilities.
> Two new packages, java.nio.file and java.nio.file.attribute , are added to improve platform independence and add support for metadata and symbolic links.
> XRender-Based Rendering Pipeline for Java2D.
> Security enhancement : Elliptic Curve Cryptography (ECC), CertPath Algorithm Disabling etc...
> Swing Enhancements : JLayer Class, Nimbus Look & Feel, ease in mixing Heavyweight and Lightweight components, Shaped and Translucent Windows, HSL Color Selection in JColorChooser Class.
> Networking Enhancements : Enhanced support for new network protocol like SCTP and Sockets Direct Protocol , addition of URLClassLoader.close method.
> XML Enhancements : inclusion of JAXP, also support JAXB, JAX-WS.
> Internationalization Enhancements : supports Unicode 6.0.0 , Support for ISO 4217 Currency Codes, Locale Class Supports BCP47 and UTR35, Unicode 6.0 Support in Regular Expressions API etc.
> Enhancement of Rich Internet Applications (RIA) : Requesting and Customizing Applet Decoration in Dragg able Applets, Embedding JNLP File in Applet Tag, Deploying without Codebase, Handling Applet Initialization Status with Event Handlers.
************************************************
Strings in Switch statement
Using JDK 7, you can pass string as expression in switch statement. The switch statement compares the passed string with each case label and execute the case block which have matched string. The comparison of string in switch statement is case sensitive.
The example code for the above is given below :
public class StringsInSwitch
{
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg)
{
String typeOfDay;
switch (dayOfWeekArg)
{
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
System.out.println(typeOfDay);
return typeOfDay;
}
public static void main(String args[]) {
new StringsInSwitch().getTypeOfDayWithSwitchStatement("Friday");
}
}
******************************
The try-with-resource Statement
The try-with-resource statement contains declaration of one or more resources. As you know, prior to Java SE 7, resource object must be closed explicitly, when the resource use or work is finished. After the release of Java SE 7, the try-with-resource statement handles it implicitly means it automatically closed the resource when it has no longer use to the class. But for this, you need to declare resources using try-with-resource statement.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class J7TryWithResource
{
static String readFirstLineFromFile(String path) throws IOException
{
try (BufferedReader br = new BufferedReader(new FileReader(path)))
{
System.out.println(br.readLine());
return br.readLine();
}
}
public static void main(String args[])
{
try {
readFirstLineFromFile("C://J7Try.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
}
*******************************
Underscores in Numeric Literals:-
In Java SE 7, You can place any number of underscore( _ ) between digits of numeric literals. This added feature improves the readability of the code. For example : Using underscore you can separate groups of digits in numeric literals . For Example credit card number can be write as follows :
long creditCardNumber = 1234_5678_9012_3456L;
You can place underscore between digits. But for placing underscore you need to be careful. You can't place underscore in the following places :
> You can't place it at the beginning or at the end of a number.
> Adjacent to a decimal point in a floating point literal.
> Before to an F or L suffix.
> In positions where a string of digits is expected.
Some of the valid example with different type of data type is given below:
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
Some of the invalid examples are given below :
float pi1 = 3_.1415F;
float pi2 = 3._1415F;
long socialSecurityNumber1= 999_99_9999_L;
int x3 = 52_;
int x5 = 0_x52;
int x6 = 0x_52;
int x8 = 0x52_;
int x11 = 052_;
*********************************
Multiple Exception Catching
In this section, you will learn about new catch method added in Java SE 7 which eliminate duplicate code.
Take a look at the following code :
catch (IOException ex) {
logger.log(ex);
throw ex;
catch (SQLException ex) {
logger.log(ex);
throw ex;
}
Prior to Java SE 7, you can't create common catch method to remove duplicate code. But in Java SE 7, you can create common catch method to remove duplicate code as follows :
public class J7MultipleExceptionCatching
{
public static void main (String args[])
{
int array[]={20,10,30};
int num1=15,num2=0;
int res=0;
try
{
res = num1/num2;
System.out.println("The result is" +res);
for(int ct =2;ct >=0; ct--)
{
System.out.println("The value of array are" +array[ct]);
}
}
catch (ArrayIndexOutOfBoundsException|ArithmeticException e)
{
System.out.println("Error....some exception occur");
}
}
}
*************************
Exceptions Rethrowing with Improved Type Checking
Take a a look at the following code having improved type checking of Java SE 7 :
Two classes FirstException and SecondException is given below :
static class FirstException extends Exception { }
static class SecondException extends Exception { }
The third class, containing improved type checking, which have the following method :
public void rethrowException(String exceptionName) throws FirstException, SecondException
{
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
}
catch (Exception e) {
throw e;
}
}
The above code will only execute using Java SE 7 , If you will try to execute it through prior version than Java 7, you will get the error " unreported exception Exception; must be caught or declared to be thrown " .
The reason behind this is the type of the catch parameter e is Exception, which is a supertype, not a subtype, of FirstException and SecondException. Throws must be like this : "throws Exception" to compile the above code prior to Java 7.
But now(in Java SE 7), you can throw an exception that is a supertype of the throws type. Here Exception is the super type of the FirstException and SecondException.
The complete Code is given below :
import java.io.*;
public class J7RethrowingException
{
public void rethrowException(String exceptionName) throws FirstException, SecondException
{
try {
if (exceptionName.equals("First")) {
System.out.println("FirstException is thrown");
throw new FirstException();
} else {
System.out.println("SecondException is thrown");
throw new SecondException();
}
}
catch (Exception e) {
throw e;
}
}
public static void main(String args[])throws FirstException, SecondException{
new J7RethrowingException().rethrowException("First");
}
}
********************************