Sunday, July 31, 2011

Catch statement has modified implementation in JDK 7

Part of coin project is modified implementation of catch block. Now multiple exception can be catached in single catch block. We can use it in following way :

try {
String str = null;
int i = 12 / 0;
System.out.println("String length : " + str.length());
} catch(NullPointerException | ArithmeticException e) /*Multiple exceptions in a catch statement*/{
System.out.println("Exception occured : " + e.getMessage());
}

As seen in above code, two exceptions are mentioned in catch block and which ever exception will be occur that will be catch.
But we cannot mention exceptions which are related by subclassing in mutli catch statement.
For e.g.
try {
String str = null;
int i = 12 / 0;
System.out.println("String length : " + str.length());
} catch(NullPointerException | ArithmeticException | Exception e) /*Not allowed*/{
System.out.println("Exception occured : " + e.getMessage());
}

In above code, Exception is parent class of NullPointerException and ArithmeticException and so these cannot be mention in same multi-catch statement.

Friday, July 29, 2011

COIN project of JDK7

One language level change is in Switch statement. Now switch statement supports string. For e.g.

String str1 = "hello";
switch(str1) {
case "hello": System.out.println("Hello!!!!");break;
default: break;
}

Earlier switch only supports integer, character, byte and short.

Java 7 .. lots of new features


Recently Java 7 has been launched by Oracle. Its a major release after Oracle acquired Sun. This new version has lot of new features and lot to explore....
Currently, I am exploring those features and will update about it here.
Also looking forward from other Java Geeks to share their thoughts here.