Some time back I
wrote a post (an angry one) on Oracle's acquisition of MySQL and Java. I saw a
take over of Open-Source by corporate world as not-so-good, however I am glad
to write off my fears. Oracle is definitely taking Java seriously and moving
forward with it. Java is growing bigger and better.
My preview into
Java7 features
1. Try-with-resources
Try-with-resources declares more than one resources in a
statement. A resource is as an object that should be closed once its no longer
required. The try-with-resources statement make sure that each resource is
closed without the hassles of extra code.
Old Way::
static String readFilePreJava7(String
filePath) throws IOException {
BufferedReader brd = new BufferedReader(new FileReader(filePath));
try {
return brd.readLine();
} finally {
if (brd
!= null) brd.close();
}
}
New Way::
static String readFileJava7(String filePath) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
{
return br.readLine();
}
}
How to use
Object should
implement java.lang.AutoCloseable, which includes all objects which implement
java.io.Closeable.
public abstract class Reader implements Readable, Closeable {
..
..
public interface Closeable extends AutoCloseable
{
You
might be tempted to declare more than one resources and you could very well do
that as well. Check for the detailed API specification and usage at Oracle site.
2. Binary Literals
A bit of a
complexity not Short but Long. Check details at Oracle Site.
3. Strings in Switch Statement
Now this is
something I've been waiting for long. Makes life oh-so-easy.
public String getDayMood(String dayOfWeek) {
String moodofDay;
switch (dayOfWeek) {
case "Monday":
moodofDay = "Not again
!";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
moodofDay = "Wait Wait
Wait";
break;
case "Friday":
moodofDay = "Wait is
over. TGIF";
break;
case "Saturday":
case "Sunday":
moodofDay = "Its over
so soon :(";
break;
default:
throw new IllegalArgumentException("Is it valid? " + dayOfWeekArg);
}
return moodofDay;
}
The String objects comparison in switch statements
is case sensitive. The Java compiler generates generally more efficient
bytecode from switch statements than if-then-else statements.
[Source: Oracle Link]
4. Improved Exception Handling and Type Checking
This one is
probably unexpected and a huge benefit in the area of code reduction.
Before::
try
{
//do something nasty here
}
catch(SQLException sqe){
// do something nastier
throw e;
}
catch(ArrayIndexOutOfBoundsException arre)
{
throw e;
}
catch(IOException ioe)
{
throw e;
}
After::
try
{
//do something nasty here
}
catch(SQLException|ArrayIndexOutOfBoundsException|IOException e){
// do something nastier
throw e;
}
Probably very
simple & self-explanatory, catch some more at Oracle Site.
5. Numeric Literals with Underscore
A great utility
feature.
Before::
long oneBillionDollar =
1000000000; // 9 Zeroes
long oneMillionDollar =
1000000; // 6 zeros
long oneHundredThousand =
100000; // 5 zeros
After::
long oneBillionDollar =
1_000_000_000; // 9 Zeroes
long oneMillionDollar =
1_000_000; // 6 zeros
long oneHundredThousand =
1_000_00; // 5 zeros
Self explanatory,
eh !. Check details at Oracle Site.
6. Type Inference for
Generic Instance Creation
So much of work
(Pre-Java7-Era):
Map> iMap = new HashMap>();
Reduced code
(Java7-Era): Automatic inference of type
Map> iMap = new HashMap<>();
Map> iMap = new HashMap();
7. Improved Compiler warnings for Varargs
Heap Pollution
Variable Arguments
Methods and Non-Reifiable Formal Parameters
Potential
Vulnerabilities of Varargs Methods with Non-Reifiable Formal Parameters
JVM Enhancements
JDBC 4.1
javax.sql.rowset.RowSetFactory interface
and javax.sql.rowset.RowSetProvider class enables to create all
types of row sets supported by the JDBC driver. Details.
Java 2D
XRender-Based
Rendering Pipeline
Support for
OpenType/CFF Fonts
TextLayout now
Supports Tibetan Script
Better Support for
Linux Fonts
Multithreaded Custom Class Loaders
The Java SE 7
release contains an important enhancement for multithreaded custom class
loaders. In previous releases, certain types of custom class loaders were prone
to deadlock. The Java SE 7 release modifies the locking mechanism to avoid
deadlock. Read the details here
...
Cool Swing Enhancements
JLayer Enhancement
Nimbus Look & Feel
Lightweight & Heavyweight Components
Translucent & Shaped Windows
URL ClassLoader
The
URLClassLoader.close method has been added. This method effectively eliminates
the problem of how to support updated implementations of the classes and
resources loaded from a particular codebase, and in particular from JAR files. See Closing a
URLClassLoader for more information.
Other Important updates
Rich Internet
Applications (RIA)/Deployment
Happy Coding !!
No comments:
Post a Comment