2. What’s new in Java 7?
New Features in Java 7
• Dynamic Languages Support.
• Project Coin.
• Fork/Join Library.
• NIO2.
• Most important features (e.g., lambda functions,
modularization) are deferred to Java 8.
4
copyright 2011 Trainologic LTD
3. What’s new in Java 7?
New Bytecode Instruction
• Java 7 introduces a change in the JVM bytecode
instructions.
• This has not been done in many releases.
• The new bytecode instruction available in Java 7 is:
invokedynamic.
4
copyright 2011 Trainologic LTD
4. What’s new in Java 7?
Dynamic Languages Support
• Java 6 introduced the Scripting API for scripting support
for dynamic languages.
• Although you can create now applications that will run
scripts in many languages (e.g., Jython, Groovy,
JavaScript) compilation of dynamic languages is a
problem.
• Now, why is that?
4
copyright 2011 Trainologic LTD
5. What’s new in Java 7?
Dynamic Languages Support
• The current bytecode instructions only provide method
invocations that are strongly types (i.e., types of
arguments must be know in advance).
• That makes a burden on compilers for dynamic
languages and often results in performance degradation
and poor generated code.
• The new mechanism allows for dynamically generated
methods to be added and invoked.
4
copyright 2011 Trainologic LTD
6. What’s new in Java 7?
Project Coin
• Project Coin introduces changes to the syntax of the
Java language.
• Note that these are not major changes as were done in
Java 5.
• Let’s see what it is about…
4
copyright 2011 Trainologic LTD
7. What’s new in Java 7?
Project Coin
• Project Coin includes:
• Support for strings in switch statements.
• Binary numeric literals.
• Improved type inference.
• Multi-catch.
• Precise rethrow.
• ARM (Automatic Resource Management).
4
copyright 2011 Trainologic LTD
8. What’s new in Java 7?
Switch on Strings
• The current JVM does not allow switch on non integer
types.
• I.e., only int, short, byte, char and Enum are allowed.
• The new version will also support Strings.
• Now, this was done without altering the bytecode.
• I.e., by using compilation techniques.
• Let’s see an example…
4
copyright 2011 Trainologic LTD
9. What’s new in Java 7?
Example
• Switch on strings:
switch (str) {
case "a":
System.out.println("is a");
break;
case "bcd":
System.out.println("string is bcd");
break;
default:
System.out.println("couldn't match");
}
21
copyright 2011 Trainologic LTD
10. What’s new in Java 7?
Binary Literals
• Currently Java supports only decimal, octal and
hexadecimal numeric literals.
• Java 7 supports also binary literals:
int a = 0b0101;
int b = 0b1010;
System.out.println(a+b);
21
copyright 2011 Trainologic LTD
11. What’s new in Java 7?
Improved Type Inference
• While the Java compiler will not provide extensive type
inference (as in Scala), a minor inference improvement
is introduced in Java 7:
List<String> strings = new ArrayList<>();
21
copyright 2011 Trainologic LTD
12. What’s new in Java 7?
Multi-Catch
• One of the annoying things about exception handling in
Java is the need to duplicate exception handling code
for different exception types.
• This is addressed in Java 7 as mulit-catch.
• Let’s see it in action:
21
copyright 2011 Trainologic LTD
14. What’s new in Java 7?
Precise Rethrow
• Currently the following code will not compile:
public void foo() throws IOException, SQLException {
try {
// do something that may throw IOException or
// SQLException
} catch (Exception e) {
// do something
throw(e);
}
}
29
copyright 2011 Trainologic LTD
15. What’s new in Java 7?
Safe Re-throw
• The syntax to allow this is with the final keyword:
public void foo() throws IOException, SQLException {
try {
// do something that may throw IOException or
// SQLException
} catch (final Exception e) {
// do something
throw(e);
}
}
30
copyright 2011 Trainologic LTD
16. What’s new in Java 7?
ARM
• Automatic Resource Management will allow you to
specify a resource (AutoClosable type) to use with the
try-catch block.
• When you exist the try-catch block the close() method
will be invoked automatically.
• Let’s see it in action…
21
copyright 2011 Trainologic LTD
17. What’s new in Java 7?
Try with Resources
static class MyClass implements AutoCloseable {
@Override
public void close() throws Exception {
System.out.println("Close was called");
}
}
private static void trywith() {
MyClass resource1 = new MyClass();
MyClass resource2 = new MyClass();
try (resource1; resource2) {
System.out.println("in try");
} catch (Exception e) {
System.out.println("in catch");
}
}
29
copyright 2011 Trainologic LTD
18. What’s new in Java 7?
Concurrency Utils
• Doug Lea, the founder of the excellent
java.util.concurrent introduces (through JSR 166) the
following new features:
• Fork/Join framework.
• TransferQueue.
• ThreadLocalRandom.
• Phasers.
18
copyright 2011 Trainologic LTD
19. What’s new in Java 7?
Fork/Join
• The basic idea of the Fork/Join framework is that many
tasks can be split to several concurrent threads, and the
result should be merged back.
• Let’s take a look at an example…
19
copyright 2011 Trainologic LTD
20. What’s new in Java 7?
Fork/Join
• Instead of doing it single-threaded, the idea of fork/join
is to split the operation to several (depends on the # of
cores) concurrent threads.
public class Fibonacci extends RecursiveTask<Integer> {
private final int n;
public Fibonacci(int n) { this.n = n;}
protected Integer compute() {
if (n <= 1)
return n;
Fibonacci f1 = new Fibonacci(n - 1);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2);
return f2.compute() + f1.join();
}
}
20
copyright 2011 Trainologic LTD
21. What’s new in Java 7?
Example
• The Main class:
public class Main {
public static void main(String[] args) {
ForkJoinPool pool = new ForkJoinPool(3);
Fibonacci fibonacci = new Fibonacci(20);
pool.execute(fibonacci);
System.out.println(fibonacci.join());
}
}
21
copyright 2011 Trainologic LTD
22. What’s new in Java 7?
TransferQueue
• A BlockingQueue on which the producers await for a
consumer to take their elements.
• Usage scenarios are typically message passing
applications.
22
copyright 2011 Trainologic LTD
23. What’s new in Java 7?
Phasers
• “Beam me up, Scotty!”
• A Phaser is quite similar to CyclicBarrier and
CountDownLatch but is more powerful and flexible.
• A Phaser has an associated phase-number which is of
type int.
• A Phaser has a number of unarrived parties. Unlike
CyclicBarrier and CountDownLatch, this number is
dynamic.
23
copyright 2011 Trainologic LTD
24. What’s new in Java 7?
Phasers
• A Phaser supports operations for
arriving, awaiting, termination, deregistration and
registration.
• When all the parties arrive, the Phaser advances
(increments its phase-number).
• Phasers also supports ForkJoinTasks!
24
copyright 2011 Trainologic LTD
25. What’s new in Java 7?
NIO.2
• JSR 203 adds new APIs to the NIO package.
• Main features:
• Filesystem API.
• Asynchronous Channels.
25
copyright 2011 Trainologic LTD
26. What’s new in Java 7?
Filesystem API
• At the heart of the new filesystem API stands the Path
class.
• An instance of Path can be thought of as an improved
version of a File instance.
• Also, File now provides a method for legacy code
named: toPath().
• Let’s explore this class…
26
copyright 2009 Trainologic LTD
27. What’s new in Java 7?
Path
• Path is an abstract representation of an hierarchical
path from the filesystem provider.
• For regular filesystems, a Path is interoperable with File.
• For other providers (e.g., ZipFileSystemProvider) there
is no analogy.
• A Path instance also supports symbolic links and
provides the method: readSymbolicLink().
26
copyright 2009 Trainologic LTD
29. What’s new in Java 7?
Watchable
• Path implements the Watchable interface.
• This allows for registering watchers that are interested
in events on the Path.
• Example for events:
• OVERFLOW, ENTRY_CREATE, ENTRY_DELETE, ENTRY
_MODIFY.
• Let’s see an example for logging which files are deleted
on a given directory…
26
copyright 2009 Trainologic LTD
31. What’s new in Java 7?
Not Included
• The following (promised) features were not included in
Java 7:
• Lambda and Closures.
• Refied Generics.
• Modularization.
• Annotations on Java types.
• Collections literals.
38
copyright 2011 Trainologic LTD
32. What’s new in Java 7?
Java 8?
• So, what is intended for Java 8?
• Well, JSR 337 (Java 8) promises the following:
• Annotations on types.
• Lambdas.
• Parallel collections and support for lambdas in
collections.
• Date & time APIs.
• Module system.
38
copyright 2011 Trainologic LTD
33. What’s new in Java 7?
Proposed Lambda Syntax
• Following is the current state of JSR 335 (Lambda
expressions for Java):
Lambda for Runnable
executor.submit( #{ System.out.println("Blah") } );
Collections.sort(people, #{ Person x, Person y ->
x.getLastName().compareTo(y.getLastName()
) });
38
copyright 2011 Trainologic LTD