Moving Java Forward: A Summary Of The Oracle Web Event
Join the DZone community and get the full member experience.
Join For FreeI'll summarise some of the main points of the Java 7 release
The Language Changes
Project Coin brought about a set of languages changes for Java 7, to help developers to improve productivity. Eclipse, NetBeans and IntelliJ all have beta support for the changes, so you can already try them out in your favorite IDE. When you read through the list, I'm sure you'll agree that all improvements leads to a more coherent, concise code.
Diamond Syntax for Calling Constructors
While generics were a great addition to Java, there is a lot of extra information required that added no value, so now we go from needing this:
Map<Integer, List<String>> daysToMonths = new HashMap<Integer, List<String>>();
To being able to use this:
Map<Integer, List<String>> daysToMonths = new HashMap<>();
The compiler uses type inference to work out what the values should be on the right hand side.
This is interesting where you have the following:
Map<?> myInfo = new HashMap<>();
The compiler infers that the value on the right is an Object. If you use
Map<? extends Number> myInfo = new HashMap<>();
the compiler is aware that the base class on the right is a Number.
Varargs Warnings
The idea of this feature is to no longer receive uninformative unchecked compiler warnings from calling platform library methods.
Multi-catch
With multicatch, your catch block can handle a number of different exceptions. So instead of having multiple catch blocks for checked exceptions like this:
try { //code } catch(IOException e) {} catch(NoSuchMethodException e) {}
You can use
try { //code } catch(IOException | NoSuchMethodException e) {}Try-With-Resources
Where you previously used the finally block in a try/catch statement to close any resources and prevent leaks, Java 7 provides a more elegant solution. This provides a new form of try block which takes resources as parameters. These resources are guaranteed to be closed:
try(InputStream in = new FileInputStream("in.txt"); OutputStream out = new FileOutputStream("out.txt")) { //code } catch(IOException ioe) { //handle exception }
Strings in Switch
In the past, you could only use integers or enums in your switch statement.
String can now be used in the switch statement. While this seems like a small change, it will make a huge change to the readability of your code, saving you from a pile of if/else statements.
Improvements To Literals
Integers can use either a hexidecimal or binary base.
You can watch the complete overview of Project Coin features in the following video:
Fork / Join Framework
The Fork/Join framework takes a divide & conquer approach to parallelism.
Let's say you have a task that you want to be able to be run in parallel. This class must extend RecursiveAction, provided by the framework.
Then, if you can split up the work that the task does, you simply call invoke all on both sides of the task you split, using the invokeAll() method
public class Task extends RecursiveAction { protected void compute() { //decide if fork necessary //if it is invokeAll (leftTask, rightTask); //else //do work } }
See the complete overview here:
New File System API
Java has been missing some basic file operations, such as copy and move for a long time now. This has resulted in developers needing to use the Apache Commons IO library, or writing similar code across projects. That's only one of the problems with the current File implemention.Now, instead of using the File object, you can use Path. This is an immutable class that can be created from a String, URI or a File.
As well as other changes, there's a nice File change notification service available called WatchService. Using this, you can watch for changes on a particular file or directory.
For more information, check out the following presentation
After Java 7
The Java 8 release will include features like Project Jigsaw (modularisation), Project Lambda (closures) and a second part of Project Coin's language enhancements.As well as that we'll be seeing full JVM convergance, with JRockit and Hotspot integrated.
Opinions expressed by DZone contributors are their own.
Comments