Java 7 awesome Features
Whenever, I am crawl through the web i notice the word java 7.Then,I really found out java 7 is today’s hot topic. So i googled it for know something about java 7.At that time i found out some thing interested and useful features in java 7.Thats push me here to share something to you all folks.Lets get into the topic.
The project name of java 7 development is Project Coin.I have listed some of the features below.
Null-safe Method invocation:
This is the greatest feature which is going to added in Java 7.NullPoniterException is one of the most common exception encountered in Java programming.
When I searched “NullPointerException” in Google, it gave about 5,570,000 results! This proves how pervasive the exception is and how much effort a developer has to put in writing java code free from null pointer exception.
Suppose if java has a feature to keep us in safe zone from null pointer exception, how is it? It happens.
Suppose we have a method to fetch postal code of a person’s address:
public String getPostcode(Person person) { if (person != null) { Address address = person.getAddress(); if (address != null) { return address.getPostcode(); }} return null; }
Now check the above syntax. We have done lots of if (null! = object) checks to avoid NullPointerException.
public String getPostcode(Person person) { return person?.getAddress()?.getPostcode(); }
Null-ignore invocation is concerned with dealing with possible null values in calling one or especially a chain of methods. Check the syntax?. while calling method on an object. This is Null-safe operator in Java 7. Thus, you can avoid lots of if (null!= object) checks in Java 7.
Strings in Switch Statements:
Whenever i am working with switch-case i felt, if the case allowed the string as a case variable we feel so comfort on that. Because the switch case allows only the primitive data types as variable such as integer, char etc.
Whenever i working with the methods that returns status of the operations like succesful, failed like that. At that time i need to convert that in to constants. Then only i can move on switch statements.
But java 7 offers the Strings as case variables in Switch Statements, So we are free from the conversion process.
Multi-Exception Catch:
Another awesome update is Multi-Exception Catch, that means a single catch can handle multiple exceptions. The syntax is
try { block of statments } catch(Exception1|Exception2|Exception3...) { block of statements. }
It save lot of spaces in the code.
Bracket Notation for Collections:
This feature refers to the ability to reference a particular item in a Collection with square brackets similar to how Arrays are accessed. Collection is one of the key concepts in java.
But when i was a student, I feel so discomfort with collections because of its strutcture.Thats some thing different from basic things in java.
But now its also be like a simple things like array etc. For ex, a Collection class we might consider something similar to arrays.
Instead of:
Collection<String> c = new ArrayList(); c.add(“one”); c.add(“two”); c.add(“three”); Use: Collection<String> c = new ArrayList {“one”, “two”, “three” };
Thats all folks.If any important updates are missing don’t hesitate to add that as comments.
There some more useful improvements, one of the most awaited (by me) is named regex capturing groups like (?X) (see http://download.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html),
Sorry to say but only one of those features made Java7 which is Strings in switch. Collection Literals will come in Java 8. But there are good stuff in Java 7, such as automatic resource management.
I agree, these features are awesome and is why I’ve used Groovy for the last 3 years. Oh wait, these are Java code samples? Well, still nice. But you can get them today in Groovy.
Really cool features..
I really like the new features in Java 7, but there are already good possibilities in Java 6 and lower.
Concerning Null-safe Method invocation:
Often it’s better to avoid nulls at all by using the “null object pattern”. See the web for descriptions.
Strings in Switch Statements:
To avoid slow if equals else if equals else if equals else … constructions use the String as a key to a HashMap to get what you really want, e.g. the values in the HashMap could be callable objects. I hope that this is the internal implementation of the new switch case statement, so that it’s really fast.
Bracket Notation for Collections:
Try this:
Arrays.asList(new String [] {“one”, “two”, “three” });
Hi Boris,
I got valuable informations from your comments.
Thanks.
@Boris Foigmann
IMHO Arrays.asList(…) returns an immutable list.
@Muneeswaran
Good article. But as mentioned in some comments, we have to wait till Java 8 to access many of these features. Nevertheless, a great effort from you.
Hi James,
Thanks for your inspirational comments.I am so happy to see you here.
No, in fact changes to the returned list “write through” to the array.
Great article – the null-safe method invocation I can’t wait for. Will make code so much less verbose.
Although the null check operator and collection literal notation were submitted to project coin, they didn’t make it for JDK 7.
The collections changes have been deferred until JDK 8 and I believe that the null check operator was rejected as a change.
Hi simon,
Thanks for your valuable information.I got that point from you.
Hi there,
Thanks for the new insights!
Finally a String switch statement!! 🙂
Eggsy
Hi eggsy,
Thanks for your valuable comment.Ya.Finally switch statement has string as case variable.
Too little, too late. These additions are of limited value and cover only very specific use cases. Look at well designed languages like Scala, Clojure etc. which don’t need special case hacks like this, but instead they can be provided by more generally applicable language constructs. What Java (the language) really needs is lambda expressions, but Sun/Oracle has messed up this completely.
Its very good
please post more about java 7..
Hi Ram,
Thanks for your valuable comments.Its increase my enthusiasm to know about java 7.Thanks.
Is that freaken all ? we have waited for years only for this ? Not even closures and function litterals. Screw Java, even Groovy is a more advanced language.
final int strHash = someString.hashCode();
switch( strHash ){
case “string in switch”.hashCode(): /*do something*/ break;
…
}
This code is not ok. Two different strings can have identical hash codes!
You have to replace /* do something */ by
if (“string in switch”.equals(someString))
{
/* do something */
}
to cover this case!
Hi Munees,
Excellent article. Keep up the good work!!
– Gift Sam
Hi,
Thanks for your motivational comment.
There is one more cool feature named Automatic Resource Management. This is new try-with-resources code-block construct that allows the runtime environment to automatically manage closeable resources e.g. streams.
For more information: http://faisalferoz.wordpress.com/2010/10/22/java-7-gets-automatic-resource-management/
Hi Faisal,
Thanks for your valid information.I don’t know,How can i miss that important feature.Anyway,Thanks for your sharing.
I had C# inflicted upon me recently – about six months back – and it already has this feature. It’s one of the things that I liked about the language, that and automagic accessor generation. My beloved Python has the “with” construct that can be used for the same purposes. I’m not a java-hater by any means (12 yrs experience with it) but it always seems to be playing catch-up.
That’s an usefull article with good examples. Thanks.
Big deal. Java’s still slow.
Well, if i am not mistaking this list is pretty outdated. Null-safe reference will not be included in java 7. Same will happen with bracket notation for collections.
Actually java 7 gives almost no significant changes. I think most people shouldn’t even bother with java 7.
Good article indeed!
Good Article……
Reblogged this on Zeeshan Akhter.
Very nice and exciting. Thanks for sharing
Hi,
Java 7 has given lot of new features and as well as additional changes in exiting features. In below link you can some same code of Java 7 each features – http://javadiscover.blogspot.com/search/label/Java%207
Everyone loves what you guys are up too. This type of clever work and exposure!
Keep up the superb works guys I’ve you guys to my personal blogroll.