Friday, 15 June 2012



Java 8
Upcoming release, not yet final.
Language changes:
·         lambda expressions (JSR 335, includes method handles)
·         continuation of Project Coin (small language improvements)
·         annotations on Java types
Library changes:
·         Improved Date and Time API

Java 7

Language changes:
·         Project Coin (small changes)
·         switch on Strings
·         try-with-resources
·         diamond operator
Library changes:
·         new abstracted file-system API (NIO.2) (with support for virtual filesystems)
·         improved concurrency libraries
·         elliptic curve encryption
·         more incremental upgrades
Platform changes:
·         support for dynamic languages
Java 6
Mostly incremental improvements to existing libraries, no new language features (except for the@Override snafu).
Java 5
Language Changes:
·         generics (that's the big one)
·         annotations
·         varargs, enhanced for loops (for-each)
Library changes:
·         concurrency utilities in java.util.concurrent
Java 1.4
Language changes:
·         the assert keyword
Library changes:
·         regular expressions support
·         NIO
·         integrated XML handling
Java 1.3
Mostly minor improvements, really.
Platform changes:
·         HotSpot JVM: improvement over the original JIT
Java 1.2
Language changes:
·         the strictfp keyword
Library changes:
·         a unified collections system
·         Swing as a new UI-System on top of AWT
Platform changes
·         a real JIT, greatly improving speed
Java 1.1
Language changes:
·         inner classes
Library changes:
·         AWT event changes
·         JDBC, RMI
·         reflection
Java 1.0
Initial release, everything is new ;-)

Code examples for new features in Java 1.7

Most of what is below come from the excellent article from Joe Wright on his blog about New language features in Java 7

Language support for collections

This is all about writing less code when you create a List, a Set or a Map. You don't have to instantiate the Object and then add the element to the Collection. You can now do it in 1 line.

List<String> list = ["item"];
String item = list[0];

Set<String> set = {"item"};

Map<String, Integer> map = {"key" : 1};
int value = map["key"];


Automatic Resource Management

Annoyed to have verbose code because of try / catch statement. You will love this one.
Indeed, this:

BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {
   br.close();
}


Become this:

try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
}

Improved Type Inference for Generic Instance Creation (diamond)

Same thing, today you specify the Generic when you declare the interface of your object and then you have to repeat yourself when you instantiate the object. You won't have to now as you can do:
Map<String, List<String>> map = new HashMap<>();

Underscores in numeric literals

Not sure this one will be useful to lot of people. You can do:

int billion = 1_000_000_000;

Strings in switch

Nothing to explain here, the title says it all.


String availability = "available";
switch(availability) {
 case "available":
    //code
    break;

  case "unavailable":
    //code
    break;

  case "merged":
    //code

  default:
    //code
    break;
}

No comments:

Post a Comment