aspose file tools*
The moose likes Features new in Java 7 and the fly likes Your favorite new Java 7 feature Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "Your favorite new Java 7 feature" Watch "Your favorite new Java 7 feature" New topic
Author

Your favorite new Java 7 feature

Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
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
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

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

Joined: Sep 20, 2010
Posts: 3649
    
  17

By far, Try-with-resources. This is going to make my code so much more clean.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

The new File API!
cc11rocks


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3091
    
  33

try-with-resources, also multi-catch blocks.


Mohamed Sanaulla | My Blog
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

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

Joined: Aug 16, 2005
Posts: 14420
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9616
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
Joe;

The target date is July 28 for official release.
Boris Mechkov
Ranch Hand

Joined: May 13, 2011
Posts: 72

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!


OCPJP 6
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Stan Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 120

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


Good luck for yourself.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

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

Joined: Apr 13, 2004
Posts: 120

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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Apr 13, 2004
Posts: 120

I told that example I provided is kind of an ugly one exactly because of reasons you mentioned.

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Sep 08, 2007
Posts: 3091
    
  33

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

SCJP
Visit my download page
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

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

Joined: Jul 21, 2011
Posts: 5

update certification
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

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/


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Vishal Baid
Ranch Hand

Joined: Jul 18, 2012
Posts: 42
    
    1
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

Joined: Jul 18, 2012
Posts: 42
    
    1
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

Joined: Sep 08, 2007
Posts: 3091
    
  33

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

Joined: Jan 29, 2007
Posts: 313

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!!


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

Joined: Jan 29, 2007
Posts: 313

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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Oct 13, 2005
Posts: 40027
    
  28
They have been promising to prohibit raw types in new code since before Java6.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

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

Joined: May 26, 2003
Posts: 31050
    
162

Bumping this for the promo about Java 7.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
Prohibiting raw types would have been nice. So would generics which people can actually understand.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

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

Joined: Jan 29, 2007
Posts: 313

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

Joined: Jan 07, 2013
Posts: 43

I've never heard of the JLayer feature (very cool ). Great blog post, Augusto!
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Try with automatic resource management
Mutli-catch block.
File change notifications.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Your favorite new Java 7 feature