• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Your favorite new Java 7 feature

 
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By far, Try-with-resources. This is going to make my code so much more clean.
 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new File API!
cc11rocks
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try-with-resources, also multi-catch blocks.
 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe;

The target date is July 28 for official release.
 
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 120
IntelliJ IDE Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
IBM DB2 Mac OS X jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
update certification
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They have been promising to prohibit raw types in new code since before Java6.
 
Palak Mathur
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bumping this for the promo about Java 7.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 57
1
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never heard of the JLayer feature (very cool ). Great blog post, Augusto!
 
security forum advocate
Posts: 236
1
Android Flex Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try with automatic resource management
Mutli-catch block.
File change notifications.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic