• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Your favorite new Java 7 feature

 
Augusto Sellhorn
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to share my blog post on 7 features I think are cool in Java 7;

"7 new cool features in Java 7"
http://sellmic.com/blog/2011/07/08/7-new-cool-features-in-java-7/

But I was wondering if I had to pick one which it would be, and what everybody would chose.

I think for me it would have to be the multiple exception handling interface. I know it's not the most exciting thing, but I've been guilty of one of the things I criticize in my blog post which is using catch (Exception e) when I'm bothered by catching 5 different exceptions because I'm doing some reflection work.

So what's your favorite Java 7 feature?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it's the try-with-resources statement. I just have way too much JDBC code which says "Do this SQL thing, maybe catch the exceptions, and finally close everything but ignore the errors which won't ever get thrown when I close those things anyway". All of those ugly finally-blocks can be blown away.
 
Stephan van Hulst
Bartender
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By far, Try-with-resources. This is going to make my code so much more clean.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new File API!
cc11rocks
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try-with-resources, also multi-catch blocks.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm happy Java is moving but nothing caught my eyes in Java 7. invokedynamic is cool any way.
I hope Java 8 will bring much more exciting and important features to the table.
Maybe it is time to announce Scala programming language as the second official language on the JVM.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of the Project Coin features, the try-with-resources is also my favorite.

Of the other features, I like the new file system API. One thing that's useful is the WatchService, which you can use to get events when there are changes to a file or directory. I've made more than one system where a directory has to be monitored for files that appear there (which then have to be picked up to be processed). You'd do that by making some timer list the contents of the directory every now and then. Now you can ask the WatchService to notify you when that happens. It will use the native functionality of the OS for this (if your OS has functionality for this), which makes it much more efficient than listing the contents of the directory and checking for changes yourself. It's also useful to make your application react when a configuration file is changed, for example.
 
Augusto Sellhorn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Todd wrote:I'm happy Java is moving but nothing caught my eyes in Java 7. invokedynamic is cool any way.
I hope Java 8 will bring much more exciting and important features to the table.
Maybe it is time to announce Scala programming language as the second official language on the JVM.


I'm not sure what making a language the "second official language" would really accomplish.

Anyways, I think the planned changes for 8 are pretty significant changes; you've got lambda expressions, literal expressions for collections and modularization via Jigsaw.

That's a good set of features that will take some time to digest ...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it is by far try-with-resources. It makes your code (and more importantly for me, code of others) much easier to read and less bug prone when handling resources.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Likewise. I never knew how much I'd like it until I posted in this thread. From 32 lines and four nested try statements to just 10 lines and one try statement, only by applying the try-with-resources syntax.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering what's so cool about Project Coin. The features offered are not so interesting. For me the fork join framework seems interesting and the invokedynamic.

By the way I thought of upgrading my project straight away with Java 7, but the Oracle website still says that it is not yet a release candidate and it is still in the early access stage. any ideas as to when a stable release will be made? I just downloaded Eclipse Indigo and Tomcat 7 and this will be my new development environment. Eagerly awaiting a stable Java 7 official release.
 
Augusto Sellhorn
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe;

The target date is July 28 for official release.
 
Boris Mechkov
Ranch Hand
Posts: 72
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Augusto Sellhorn wrote:Just wanted to share my blog post on 7 features I think are cool in Java 7;

"7 new cool features in Java 7"
http://sellmic.com/blog/2011/07/08/7-new-cool-features-in-java-7/

But I was wondering if I had to pick one which it would be, and what everybody would chose.

I think for me it would have to be the multiple exception handling interface. I know it's not the most exciting thing, but I've been guilty of one of the things I criticize in my blog post which is using catch (Exception e) when I'm bothered by catching 5 different exceptions because I'm doing some reflection work.

So what's your favorite Java 7 feature?

Pretty cool blog mate!
Excellent work!
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really excited about the Objects class that was added in. There are some really interesting and useful features inside. If you haven't seen anything about it there is a good blog post about it I read this morning: http://marxsoftware.blogspot.com/2011/03/jdk-7-new-objects-class.html


Hunter
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Nothing looks too exciting. Try with resource might be a good thing but it can only work for system classes and from language perspective it is a bad thing to have a discrimination of application code versus system code. For example if I have my class


Because my class does not have a method with signature close() it could not be used for "Try as a Resource". Sun/Oracle should not even go there. If they wanted to do stuff like this they rather add some sort of finalizer(outofscope) method for every object. It can do whatever is needed when visibility of an object got reduced. For example once BufferedInputStream got 0 visibility (but not yet to be garbage collected) it must close all resources. No try/catch should even be required.




So now objects of my class have a feature to close all open resources once they know that there is no more references. It should not have any association with a garbage collector or try catch block. Out of scope ? Good buy! No delayed invocation! No connection to context. It should be called right after last reference to an object was set to null.


2. Generic type inference handling is nice feature that I was long waiting for. But once said A why not to say B. Why not make it legal final x=""; or public b=true; It is clear from the context that x is a string and b is a boolean
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stas Sokolov wrote:Try with resource might be a good thing but it can only work for system classes...


I don't see why. As far as I can tell it can work with anything which implements the AutoCloseable interface, and anybody can write code to do that. It wouldn't be surprising to find that the code you wrote three years ago doesn't implement AutoCloseable, but there isn't anything to prevent you from making it do that. Except of course for management requirements, which you can't blame Oracle for.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Stas Sokolov wrote:Try with resource might be a good thing but it can only work for system classes...


I don't see why. As far as I can tell it can work with anything which implements the AutoCloseable interface, and anybody can write code to do that. It wouldn't be surprising to find that the code you wrote three years ago doesn't implement AutoCloseable, but there isn't anything to prevent you from making it do that. Except of course for management requirements, which you can't blame Oracle for.


Believe me or not I am fine about AutoCloseable interface. It would be no pain to implement it but I am not getting why it should be connected to try/catch block. AutoClose the class when no references left and we have all scores settled.

Look at this ugly code bellow. There are 3 resources to open. Are you going to try with resource for all of them ? Keep in mind code bellow is ugly. I found it online. close() can trigger an exception and it technically should be surrounded by try/catch of its own. How nice it would be that every AutoClose variable would be closed once last reference to it is gone. It would be nice to have a new access modifier for methods autoclose that would allow this type of behavior. For simplicity it can work only for method scope variables and close() call be generated by a compiler during the compilation time at every place where method local variable is used last time.


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you get how to use try-with-resources. It would be something like the following:


And the code you posted doesn't close the resources properly. If rs.close() would throw an IOException then the statement and the connection will not be closed.
 
Stan Sokolov
Ranch Hand
Posts: 120
Hibernate IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told that example I provided is kind of an ugly one exactly because of reasons you mentioned.

 
Stephan van Hulst
Bartender
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Requiring instances to become unreachable before they are closed is a very bad idea. This means resources will only be reclaimed when the garbage collector kicks in, which does not always happen, let alone in a timely manner.

Besides, you might still want to retrieve status information from reclaimable resources after they are closed, so you will want a reference to them.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Along with try-with-resources there's another API added to get the list of suppressed exceptions. Pre Java 7 the exception thrown last would be carried over to the calling method => Suppose the exception is thrown in try block, then in the finally block there's another exception thrown, the caller would come to know about the exception thrown in the finally block. With suppressed exceptions the exception thrown in the try block would have list of the exceptions it suppressed before its caught and handled.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. Generic type inference handling is nice feature that I was long waiting for. But once said A why not to say B. Why not make it legal final x=""; or public b=true; It is clear from the context that x is a string and b is a boolean


things seem to be moving that way. javaFX allows both those examples
def x = "";
var b = true;

the compiler figures out the type.

you can even say
var x = "";
println x;
x = 27;
according to someone in the FX forum this actually compiles and will print 27
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stas Sokolov wrote:Believe me or not I am fine about AutoCloseable interface. It would be no pain to implement it but I am not getting why it should be connected to try/catch block. AutoClose the class when no references left and we have all scores settled.


You already have that, it's been in Java since the beginning of time. You can write a finalize() method which is called when the garbage collector determines that no references are left and have it close your resource.

But I get the feeling that you're going to say that you want this to happen instantly when there are no references left, not some time later when the garbage collector gets around to noticing that. Which of course is a much harder problem and I'm not surprised the language designers didn't come up with a solution for that.
 
Carlos Salazar
Greenhorn
Posts: 5
IBM DB2 jQuery Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
update certification
 
Krishna Srinivasan
Ranch Hand
Posts: 1872
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My favorite feature is String in the switch statements
http://www.javabeat.net/examples/2012/05/02/whats-new-in-java-7-features-as-part-of-project-coin/
 
Vishal Baid
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really all the features are cool. But out of these multiple exception handling and try-with-resources features will help us a lot.
 
Vishal Baid
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In FInally block you neeed to close connection which we dnt require now.
I don't think you get how to use try-with-resources. It would be something like the following:


And the code you posted doesn't close the resources properly. If rs.close() would throw an IOException then the statement and the connection will not be closed.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Baid wrote:
I don't think you get how to use try-with-resources. It would be something like the following:
...

How do you think is the code posted by you different from the one posted by Wouter? I see they both are same.

Vishal Baid wrote:And the code you posted doesn't close the resources properly. If rs.close() would throw an IOException then the statement and the connection will not be closed.

There is no close invoked in any of the two code samples. And the AutoCloseable takes care of this exception being thrown by rs.close().
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Vishal Baid wrote:
I don't think you get how to use try-with-resources. It would be something like the following:
...

How do you think is the code posted by you different from the one posted by Wouter? I see they both are same.

Vishal Baid wrote:And the code you posted doesn't close the resources properly. If rs.close() would throw an IOException then the statement and the connection will not be closed.

There is no close invoked in any of the two code samples. And the AutoCloseable takes care of this exception being thrown by rs.close().


I think Vishal forgot to quote Wouter's post. Because only lines that are written by Vishal in his post are:-
In FInally block you neeed to close connection which we dnt require now.

and
And the code you posted doesn't close the resources properly. If rs.close() would throw an IOException then the statement and the connection will not be closed.


The rest is the part of Wouter's post!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the facility in Java7 (or Java7 fora) to copy the same post and for several people to post things identical . . .
How about List<String> words = new ArrayList<>();
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Apart from the facility in Java7 (or Java7 fora) to copy the same post and for several people to post things identical . . .


Actually, the two features - Try-multiple catch and Try with resources are the things that have been covered extensively on blogs and different forums. As it is easy to understand them. There is something called Parkinson's Law of Triviality [kidding ].


How about List<String> words = new ArrayList<>();


Yes, this is amazing. When generics were introduced, I used to wonder why can't the compiler understand this thing by itself. Finally it is done and it is really a cool feature as it avoids the repetition of types from LHS to RHS. If I had to make a Map of String and a Map in Java 6, then I had to write:-

Map <String, Map<String, String>> map = new HashMap<String, Map<String,String>>();

With Java 7, I just need to worry about LHS, as RHS will be automatically taken care of:-

Map <String, Map<String, String>> map = new HashMap<>();

P.S.:- I still find the <> on RHS side not necessary. They should have also been removed. But so far so good.

 
Stephan van Hulst
Bartender
Posts: 6320
78
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They couldn't. The <> on the right means you're using a fully parameterized type. If you leave it out, that means that you're using a raw type. Java still needs raw types to deal with situations in which legacy code plays a role, sadly.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They have been promising to prohibit raw types in new code since before Java6.
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:They have been promising to prohibit raw types in new code since before Java6.


Any idea if this is going to be implemented in Java 8 or Java 9?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bumping this for the promo about Java 7.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prohibiting raw types would have been nice. So would generics which people can actually understand.
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Prohibiting raw types would have been nice. So would generics which people can actually understand.


Exactly. Generics have been a pain to understand and more often than not, I have made many mistakes initially while using them.

 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one of the important features of Java 7 platform (JVM) is support of Non-JVM Languages specially Dynamic Languages. This is really an interesting change to the platform itself.

Coming back to the language, they have made changes to NIO, which is now called NIO 2.0. It includes a new filesystem interface, API for asynchronous I/O Operations on both sockets and files, etc.
 
Bernhard Goetz
Ranch Hand
Posts: 57
1
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never heard of the JLayer feature (very cool ). Great blog post, Augusto!
 
Sai Hegde
security forum advocate
Ranch Hand
Posts: 212
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try with automatic resource management
Mutli-catch block.
File change notifications.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic