10
Apr/10
Follow iNebium on twitter

Java 1.7 - What's new? Release date, code examples and performance

[10 Apr 10 at 09:17:26 - 53 comment(s)]

I have been reading quite a lot about Java 1.7. There are articles about what's new, some code examples, some benchmark to compare performance with previous version of Java and discussion on when it will be released. I have decided to regroup all I have discovered in this article so that I and maybe you, won't have to spend hours surfing the web to find all this information. Don't hesitate to leave a comment if I missed something.

What's new in Java 1.7?

First thing first. To determine what set of small language changes should be added to JDK 7, a project has been set up called Project Coin. The final five changes (bit more than 5) are described on the following blog entry of Joseph D. Darcy.

So what made it through is the following:

  • Strings in switch
  • Automatic Resource Management
  • Improved Type Inference for Generic Instance Creation (diamond)
  • Simplified Varargs Method Invocation
  • An omnibus proposal for better integral literals
  • Language support for Collections
  • Language support for JSR 292

There is a list of other features available on the OpenJDK 7 features page.

These features are divided in different categories:

VM

  • Compressed 64-bit object pointers
  • Garbage-First GC (G1)
  • JSR 292: VM support for non-Java languages (InvokeDynamic)

lang
  • SR 294: Language and VM support for modular programming
  • JSR 308: Annotations on Java types
  • JSR TBD: Small language enhancements (the Project Coin I was talking about)
  • JSR TBD: Project Lambda

core
  • Modularization (Project Jigsaw)
  • Upgrade class-loader architecture
  • Method to close a URLClassLoader
  • Unicode 5.1
  • Concurrency and collections updates (jsr166y)
  • JSR 203: More new I/O APIs for the Java platform (NIO.2)
  • SCTP (Stream Control Transmission Protocol)
  • SDP (Sockets Direct Protocol)
  • Elliptic-curve cryptography (ECC)

client
  • XRender pipeline for Java 2D
  • Forward-port 6u10 deployment features
  • Create new platform APIs for 6u10 graphics features
  • Nimbus look-and-feel for Swing
  • Swing JLayer component

web
  • Update the XML stack

As you can see there is a lot of stuff. I personally tried the new Garbage Collector (G1) a few months back and was really impressed by the performance. Unfortunately the JVM was crashing every few hours so it couldn't be used for production. This GC is available in Java 1.6 as well but same thing, it crashes every so often.

I think that's it for the new features. Maybe it's a good idea to see some code examples now.

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;
}

Binary literals

You can create binary literals using the 0b prefix

int binary = 0b1001_1001;

That's it for the code examples. It would be great if someone can point me on more things. I am sure there are plenty of other cool stuff.

Performance of Java 1.7

I have picked up the tests I ran from an article from Taranfx posted here.

So the tests are the following (my code, not his but I followed the same ideas). I ran all of this on a Macbook Pro running ArchLinux (the one with an Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz. I think 2 years old). I have 2Gb of RAM. I set the Heap Size to 728m (-Xms728m -Xmx728m).

Test 1. Add 1 Million String values to a List (the String is a UUID generated using UUID.randomUUID()).

Test 2. HashMap with 1 million keys, values. Each key, value pair is being calculated via concurrent thread. The key is a UUID and the int is generated using Math.random()

Test 3. Printing 1 million items of ArrayList to number of Files (1000). Writing to different files happening in parallel.

I am just comparing Java 1.6 (1.6.0_19) vs Java 1.7 (b87). I added Java 1.5 to this benchmark following a request in the comments but not Java 1.4 as it is from an other age to me.

So here is the result

Performance benchmark Java 1.5 vs Java 1.6 vs Java 1.7

Java 1.5 Java 1.6 Java 1.7
Test 1 10,698 sec 9,481 sec 9,328 sec
Test 2 69,827 sec 37,935 sec 36,636 sec
Test 3 26,931 sec 30,868 sec 27,383 sec

The difference of performance is clearly not as big as it is in the article of Taranfx. It appears that our implementation of his tests are probably quite different as well as my tests are taking a lot more time than his.

Release date of Java 1.7

In November 2009, it was planned to be in September 2010 as there would be 3 more milestones. Milestone 6 was completed with build 84. b85 was scheduled for the 4th of March 2010, first build of Milestone 7. b87 used in this article has been released the 25th of March 2010. So it looks like a release for September 2010 is more than likely. Though I have been reading an interesting subject on the stackoverflow forum here. You can check the blog of Alex Miller as well. He publishes links to all blog and news items referring to the progress of Java7


Add new comment





Notify me of follow up comments

53 Comment(s)

Javin @ Java Enum Example  -  14 Jan 2012 at 17:31:40
down  up
Java7 has several feature which developer would love one of them is fork-join framework which allows to break a bigger task into smaller task and then combine the result, now this is in built in java which will leverage multi processor architecture, this article is good introduction of fork-join framework in java and explains how to use it.

<a href="http://javarevisited.blogspot.com/2011/09/fork-join-task-java7-tutorial.html">fork-join in java7</a>

Javin @ Java Enum Example  -  14 Jan 2012 at 17:27:47
+1 vote(s) down  up
Java7 has several feature which developer would love one of them is fork-join framework which allows to break a bigger task into smaller task and then combine the result, now this is in built in java which will leverage multi processor architecture, this article is good introduction of fork-join framework in java and explains how to use it.

<a href="http://javarevisited.blogspot.com/2011/09/fork-join-task-java7-tutorial.html">fork-join in java7</a>

Ashwini  -  15 Dec 2011 at 06:52:46
+1 vote(s) down  up
thanks for your post. I guess try/multi-catch has also been included in 7th version of Java.
try{
}catch(NoSuchMethodException | SecurityException e){
e.printStackTrace();
}

mouli  -  07 Dec 2011 at 04:00:36
down  up
Thanks for your post

Java Enum ValueOf  -  24 Nov 2011 at 04:11:00
+1 vote(s) down  up
Indeed good examples. its quite convenient they should have introduced this when allow enum in Switch case. Allowing <a href="http://javarevisited.blogspot.com/2011/08/string-switch-case-jdk7-example.html">String in Switch Case Java7</a> makes code more readable and avoid an extra step of conversion between int and String.

Venkata Kumar  -  29 Sep 2011 at 13:03:31
-1 vote(s) down  up
Over all there is little difference in the performance over 1.6 and really there is big difference over 1.5. This Article clearly gave info on three versions of java with respective performance. Sample code snippet is good to understand what is has been newly added.
Over and all we do love JAVA in so many aspects.

SANFORD.  -  20 Sep 2011 at 19:28:06
down  up
When java when? 1.7 is like the Duke Nukem Forever release lol. Great article!

Jeremy  -  20 Sep 2011 at 00:58:28
+2 vote(s) down  up
When will the Java.com website be updated to push JRE 7 as the new update to Java for all users?

Shah  -  08 Sep 2011 at 09:52:27
-2 vote(s) down  up
It got released on July 28, 2011 with major updates of 7.

prewin  -  23 Jul 2011 at 08:08:57
-1 vote(s) down  up
Wow...very nice...when it will release?

balcerman  -  30 Jun 2011 at 15:34:19
+4 vote(s) down  up
Very useful summary introduction - thanks :)

Vlad  -  11 Apr 2011 at 22:43:50
-9 vote(s) down  up
@Raul and @rhume

It should rather be as concise as

String myString = null;
if ("Steve".equals(myString)) {
// do something
}

;)

Harga Jual Blackberry iPhone Laptop Murah  -  28 Mar 2011 at 06:01:17
-8 vote(s) down  up
this is what i waiting for, I'd really like it if Java was more forgiving about null pointer exceptions. Thank's to informative and interesting post

Bleach Wallpapers  -  04 Mar 2011 at 07:02:12
-1 vote(s) down  up
this is what i waiting for, the new release JAVA 7, and this is very suitable for my thesis, thanks for share

shahid  -  16 Feb 2011 at 09:50:42
-1 vote(s) down  up
when will switch start working on a range of values(expression) rather than workings just on constants .. for Example

int marks;

switch(marks)
{

case > 80: //do something
break;

case > 70: //do something
break;

case > 50: //do something
break;


default: //do something
break;


}



Dhiraj Bhavsar  -  20 Dec 2010 at 12:57:12
+8 vote(s) down  up
What about the unicode character for New Sign of Indian currency?

java fan  -  01 Nov 2010 at 23:33:46
+2 vote(s) down  up
what about adding more concurrency data structures, and adding the famous 'atomic' keyword (which uses Transactional Memory) to make data structures "magically" concurrent in super easy way?
will there be those features? i can't wait to see such things.


someone  -  08 Sep 2010 at 13:04:20
-3 vote(s) down  up
getters + setters...
they **ONLY** make sense, if you add some validation/checks.
these things compilers cannot do for you.
No checks in setters/getters -> do not even create them.

sudhakar  -  03 Sep 2010 at 04:28:41
+5 vote(s) down  up
thnx
can u give me the exact release date of java 1.7

Adam Gent  -  29 Aug 2010 at 12:07:37
-16 vote(s) down  up
All I wanted was closures. The switch on a string is retarded compared to something like Scala's pattern matching. I mean seriously they enhanced switch an old programming construct that has caused some serious famous bugs (at&t forgetting the break;).

With closures the switch could easily be replaced with:
Map<String, Func<Integer>> map = {"a" : it -> { do something}, "b" : it -> { do something} };

Even if/else would be better than switching on a string. But hey performance improved.


Zeus (JAVA/PHP)  -  17 Aug 2010 at 07:13:04
+1 vote(s) down  up
People by their nature always moving in simple direction even programmers - so java do everything in this matter + improves speed execution and don`t forget about key value of cross-platform =)

Raj Malkhede  -  11 Aug 2010 at 09:47:02
-2 vote(s) down  up
Thanks for giving Wonderful Information about JAVA 1.7

Dale Brenner  -  20 Jul 2010 at 16:51:38
-3 vote(s) down  up
My understanding was that there were methods added to the URLClassLoader (or a base class) to unload a previously loaded class. Did that make it to JRE 1.7

JSF Programmer  -  24 Jun 2010 at 22:39:27
down  up
I love getters and setters. And to begin with, they are called "methods"... and methods "perform" actions, right?

If Java will have @property instead of getters() and setters(), I think it will neglect it's original purpose of object orientation.

Java advocate  -  24 May 2010 at 18:59:54
+23 vote(s) down  up
@Anonymous
indeed .net is more like a black box than Java, but what really outrages me is copying features from C# just for the sake of keeping up with Microsoft's hype, rather than address the real needs of the developers.
At least Microsoft learned some good things from Java's history when creating C#, but the Java crowd does not seem to learn anything from Microsoft's mistakes, like the Managed C++ debacle, that screwed up completely a perfectly good language just for the sake of 'enriching' it.
I have yet to hear a good opinion about Managed C++, and I fear the same would happen to the 'improved' Java some guys are peddling down out throats.

Anonymous  -  24 May 2010 at 18:15:04
+19 vote(s) down  up
I agree with Mr Java advocate and there is one thing people forget about C#: it's a blackbox. As far as I know you don't have things like visualvm and you are not able to tune the VM.

Java advocate  -  24 May 2010 at 17:13:17
+20 vote(s) down  up
Mr. Frits Jalvingh you either don't know what you are talking about or have some hidden agenda.
You sound exactly like the MSFT propaganda machine: bloated is better.
C# is just a fat bloated java lame atempt to impress the ignorants.
Useless features like delegates ( closures) and syntactical sugar would only slow the language adoption and focus resources outside of framework and library innovation.
Java should stay true to its original strenghts, not try to be a 'me to' copy of C# with no noticeable benefit.
If you need a recent example of taking a good language and making it unusable, look no further than C++ -> Managed C++ blunder.
I would not want the same 'enrich the language' fad to happen to java.

Frits Jalvingh  -  28 Apr 2010 at 22:43:02
-29 vote(s) down  up
What a joke… More than 3 years and this is what is presented as improvements…. They are not: they are minor fixes of negligible value! All important proposals have been refused and we are left with this. Java is dying from neglect, and from horrible choices.

Young people are considering Java the new Cobol – a dying old-fashioned language that is no fun to work in. And they are right. Luckily the Java ecosystem is huge so it’s death will take a long time. But it will come soon enough if this stagnation does not stop.

It’s actually very, very sad. It is very possible to extend your language in new ways as C#’s history shows you. Its versions add possibilities that Java’s designers apparently do not understand- so they see no value. If you leave idiots to design your language you get one only an idiot will use.

Let’s hope Oracle’s takeover of Sun will out Java’s current stewards and bring in new blood- with brains and vision. Although I fear, knowing Oracle’s tracksheet, that this will prove not to be the case.

Anonymous  -  23 Apr 2010 at 09:17:33
+5 vote(s) down  up
actually he'll prefer something like the following
if("Steve".equals(myString)){
// do something
}

Raul  -  22 Apr 2010 at 22:30:34
down  up
@rhume: Your post about forgiving null pointers is preposterous. An implementation of your idea would create such an overhead on the VM. What you're saying is that NULL, instead of being NOTHING, it would be an implementation of the object being instantiated with a hint of NOTHING. That's the only way you could conjure a method (such as .equals()) that wouldn't exist in the NULL.

Also, nullifying objects is a good practice and eases the Garbage Collector job. So NULL has its uses.

A good developer always thinks in the worst case scenario, so, instead of complying about NULL forgiveness, shell out a couple of additional lines of codes for validation. For example, by using a short circuit AND, you could avoid errors:

String myString = null;
if(myString != null && myString.equals("Steve")){
// do something
}

Just like that, you get around one of your problems.

rhume  -  17 Apr 2010 at 03:00:10
-15 vote(s) down  up
I'd really like it if Java was more forgiving about null pointer exceptions.

For example, in ...

String myString = null;
if (myString.equals("Steve")) {
// do something
}

The myString.equals throws a null pointer -- is it really necessary or helpful? If I really care about the null pointer then I should have the *option* of catching it by wrapping it in a try ... catch. But when the try ... catch isn't there, why can't Java just evaluate it as false?

Similarly ...

List<Strings> myStrings = null;
for (String s : myStrings) {
// do something
}

... I'd prefer this for statement to not throw a null pointer exception (unless I explicitly catch it).

John Goering  -  16 Apr 2010 at 08:45:03
down  up
Umm...what happened to closures?

Daniel Rochetti  -  15 Apr 2010 at 08:36:34
-3 vote(s) down  up
Look, just to be clear, I'm not saying that the language should change, I think the compiler should generate the accessors and mutators, and we gonna use them the same way we do nowadays:
bean.getProperty().getNestedProperty();
Just no need to write those methods anymore...

Daniel Rochetti  -  15 Apr 2010 at 08:32:53
-2 vote(s) down  up
@inebium
I'm impressed about the fact that you don't understand a bunch of methods, generated by an IDE, just for simple property read/write. Yeah, my IDE of choice (Eclipse) generates all of those, including default javadoc (which is useless in getters and setters BTW), but the question is WHY? Why have a JavaBean with 10 lines of property declarations and 100 lines of getters and setters? Furthermore, it should be an easy task for the compiler to generate those... don't the DRY concept tells you anything here? Don't repeat yourself VERSUS repeated getters and setters all over the place... hmmmm

Well, I hope you tell me the advantages of having all those getters and setters instead of a "smart" property handling by the compiler...

I'm scared now, I tought every single Java guy was against the 'getters' and 'setters' hell, but even those creep methods have its fans!

Just in time, I want to correct something in my examples:
First, 'getter' and 'setter' ARE... sorry about that, I'm not a native speaker.
Second, in the readOnly example, No 'setter' is created.

inebium  -  15 Apr 2010 at 06:36:45
-5 vote(s) down  up
I don't understand why people all the time complain about getters and setters. Don't you use an IDE which is writing all the code for you?

Daniel Rochetti  -  15 Apr 2010 at 00:05:58
+7 vote(s) down  up
Java REALLY REALLY needs something to simplify the JavaBean programming style, widely used in Java World. Why not something similar to Objective-C?

@Property // 'getter' and 'setter' is created at compile time
private String name;

@Property(readOnly = true) // No 'getter' is created
private Integer age;

@Property(getter = "provided") // a default 'String getNotes()' is provided, not generated
private String notes;

and so on...
We use JavaBeans heavily, and all those generated getters and setters make me sick... we should implement them only when needed, some encapsulated data handling.

pvarma  -  14 Apr 2010 at 17:59:21
-1 vote(s) down  up
you forgot multi-catch by Neil Gafter.

try {
doWork(file);
} catch (final IOException | SQLException ex) {
logger.log(ex);
throw ex;
}

why not catch (Exception1, Exception2 e) ?

minor change but that would be more natural.

Wayne  -  14 Apr 2010 at 17:38:43
-1 vote(s) down  up
The legend on your graph says Java 1.4, 1.5, 1.6 when you should have Java 1.5, 1.6, and 1.7 -- right?

Dhruba Bandopadhyay  -  13 Apr 2010 at 11:08:28
-1 vote(s) down  up
A JDK7 NIO2 detailed introduction http://dhruba.name/2009/09/06/jdk7-nio-2-fs-api-primer/.

inebium  -  12 Apr 2010 at 23:53:24
down  up
I have added Java 1.5 to the benchmark. I have to say I'm surprised by the result. As expected Java 1.5 is slower in Test 1 and Test 2 but faster in Test 3 for some reason...

Anonymous  -  12 Apr 2010 at 17:14:09
down  up
Might as well add Java 1.4 and 1.5... it'd be interesting.

Mario  -  12 Apr 2010 at 08:53:40
down  up
Nice article,
i dont know what to think about the string-switch, it makes stuff easier but it also promotes bad coding practices - i think enums are more safe, less likely to brake the code

sboulay  -  12 Apr 2010 at 02:45:10
-1 vote(s) down  up

Anonymous  -  11 Apr 2010 at 20:18:58
+1 vote(s) down  up
For the Automatic Resource Management example, the initial code would even be :

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

...

Paul  -  11 Apr 2010 at 18:11:52
-1 vote(s) down  up
Note that the literal collection syntax, last I heard, creates read-only collections.

Osvaldo Pinali Doederlein  -  11 Apr 2010 at 18:08:29
down  up
@Marc: G1 will be freely available; in fact it is already available on latest JDK6. Indeed it's still buggy (marked "early-access"), but they are continuously back-porting the latest fixes/improvs from JDK7 to JDK6. Moreover, all G1 code is also continuously contributed to the OpenJDK project, so even if Oracle moves some JDK feature to commercial status, we can just use the free OpenJDK builds.

Marc  -  11 Apr 2010 at 17:19:18
-1 vote(s) down  up
"JSR 203: More new I/O APIs for the Java platform (NIO.2)"
It's there

Ernest  -  11 Apr 2010 at 17:11:11
-1 vote(s) down  up
What about NIO2?

Marc  -  11 Apr 2010 at 14:07:19
down  up
I am skeptical as well on number of things especially since Oracle bought Sun. For example maybe the G1 garbage collector will be available through paying a license like they've just done with Solaris

Chris  -  11 Apr 2010 at 13:20:39
-4 vote(s) down  up
I'm sceptical that we will see all of these language features. Currently there is no progress on either "Language support for collections" or "Automatic Resource Management". More work is going into Lambda expressions right now, that’s good--at least.

Alex Miller  -  11 Apr 2010 at 12:19:37
+3 vote(s) down  up
@sboulay: multi-catch will not be part of JDK 7.

sboulay  -  10 Apr 2010 at 19:12:50
+5 vote(s) down  up
annotation on types in another one.

List<@NonNull Object>)

sboulay  -  10 Apr 2010 at 19:07:35
+9 vote(s) down  up
you forgot multi-catch by Neil Gafter.

try {
doWork(file);
} catch (final IOException | SQLException ex) {
logger.log(ex);
throw ex;
}

Follow the RSS feed Follow the RSS feed

Recent Posts

How to run glxgears at full speed and not synchronized to vertical refresh?
You normally use glxgears to check performance of your graphic card but you get the same as your mon...
15 Nov 11
catalyst 11.10 / gnome-shell still not usable
In my post about catalyst 11.9, I wrote a rant about the AMD catalyst driver which after 6 months we...
01 Nov 11
A list of useful regexp
regexp or in other words regular expression is something you have to regularly use in your applicati...
30 Oct 11
catalyst 11.9 still doesn't work with gnome-shell!
Gnome 3.0 was released on the 6th of April 2011. At the time I write this post, Gnome 3.2 has been r...
29 Sep 11
3DES Encryption in Java and DES Encryption in Java
Here is a small post with just code to do 3DES (Triple DES) and DES Encryption in Java. You can simp...
13 Nov 10

Recent Comments

Unknown catalyst version 11.11 and still I have screen flickering, especially with something like banshee....
22 Jan 12 at 12:49:54
Javin @ Java Enum Example Java7 has several feature which developer would love one of them is fork-join framework which allows...
14 Jan 12 at 05:31:40
Javin @ Java Enum Example Java7 has several feature which developer would love one of them is fork-join framework which allows...
14 Jan 12 at 05:27:47
Vijay Hi,
This is the best tutorial I've come across so far to integrate maven with Eclipse even t...
27 Dec 11 at 11:07:13
inebium export vblank_mode=1...
25 Dec 11 at 01:19:14

Categories

All (22)
Android (2)
Java (6)
Linux (14)
Load Testing

Pricing

Sign Up For Free

+420 602 318 949 English / French (GMT+1)

Follow iNebium on twitter Follow us on Twitter

© iNebium 2009-2012 - All rights reserved