Java 7 Changes, Features and Enhancements: Improved Type Inference
Java 7 Changes, Features and Enhancements: Improved Type Inference
Java 7 Changes, Features and Enhancements: Improved Type Inference
I have already covered many java 7 changes which were new in the release. In this post, I am
creating a summary of them so that if anyone interested can take a quick look into all features in
short time.
Compiler is smart enough in java 7 to identify that blank diamond infer to type defined on left
hand side of declaration.
NIO 2.0
Java SE 7 introduced java.nio.file package and its related package,
java.nio.file.attribute, provide comprehensive support for file I/O and for accessing the
default file system. Path class has been a big addition which allow you to represent any path in
operating system in uniform way. New APIs complements older one and provides several useful
method checking, deleting, copying, and moving files. You can also create symbolic links and
hard links like in linux. JDK 7 new file API is also capable of searching for files using wild
cards/regex. You also get support to watch a directory for changes.
Suppressed exceptions
Suppressed exceptions, as name suggest, are exceptions thrown in the code but were ignored
somehow. If you remember try-catch-finally block execution sequence and how they return any
value or exceptions, you will recall that exceptions thrown in finally block are suppressed is
exception is thrown in try block also. Before java 7, you was informed about these exceptions by
logging if implemented, but you didn’t have any control over these types of exceptions once
finally block is over. With new features in java 7 you got control over these suppressed
exceptions as well.
Output:
try
{
//Do some processing which throws NullPointerException; I am sending
directly
throw new NullPointerException();
}
Now you can write above number like this : 10_00_000. Good enough, Isn’t it??
/**
* Supported in int
* */
int improvedInt = 10_00_000;
/**
* Supported in float
* */
float improvedFloat = 10_00_000f;
/**
* Supported in long
* */
float improvedLong = 10_00_000l;
/**
* Supported in double
* */
float improvedDouble = 10_00_000;
switch (token)
{
case ("one"):
return "Token one identified";
case ("two"):
return "Token one identified";
case ("three"):
return "Token one identified";
case ("four"):
return "Token one identified";
default:
return "No token was identified";
}
ForkJoin Framework
The effective use of parallel cores in a Java program has always been a challenge. There were
few home-grown frameworks that would distribute the work across multiple cores and then join
them to return the result set. Java 7 has incorporated this feature as a Fork and Join framework.
Basically the Fork-Join breaks the task at hand into mini-tasks until the mini-task is simple
enough that it can be solved without further breakups. It’s like a divide-and-conquer algorithm.
One important concept to note in this framework is that ideally no worker thread is idle. They
implement a work-stealing algorithm in that idle workers steal the work from those workers who
are busy.
It’s based on the work of Doug Lea, a thought leader on Java concurrency. Fork/Join deals with
the threading hassles; you just indicate to the framework which portions of the work can be
broken apart and handled recursively. It employs pseudo-code (as taken from Doug Lea’s paper
on the subject):
Now with java 7, things have changed. Java 7 has introduced an excellent feature:
WatchService. A WatchService is JDKs internal service which watches for changes on
registered objects. These registered objects are necessarily the instances of Watchable interface.
When registering the watchable instance with WatchService, we need to specify the kind of
change events we are interested in.