Home > Java 7 > Java 7 awesome Features

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.

About these ads
  1. November 16, 2010 at 8:34 am | #1

    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),

  2. ahmet
    November 16, 2010 at 8:57 am | #2

    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.

  3. November 16, 2010 at 9:18 am | #3

    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.

  4. November 16, 2010 at 9:26 am | #4

    Really cool features..

  5. November 16, 2010 at 9:40 am | #5

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

    • November 16, 2010 at 12:39 pm | #6

      Hi Boris,

      I got valuable informations from your comments.
      Thanks.

    • December 21, 2010 at 12:44 am | #7

      @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.

      • December 21, 2010 at 1:55 pm | #8

        Hi James,

        Thanks for your inspirational comments.I am so happy to see you here.

      • January 20, 2011 at 11:38 am | #9

        No, in fact changes to the returned list “write through” to the array.

  6. Ben
    November 16, 2010 at 10:53 am | #10

    Great article – the null-safe method invocation I can’t wait for. Will make code so much less verbose.

  7. Simon
    November 16, 2010 at 11:13 am | #11

    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.

    • November 16, 2010 at 12:37 pm | #12

      Hi simon,

      Thanks for your valuable information.I got that point from you.

  8. November 16, 2010 at 11:35 am | #13

    Hi there,

    Thanks for the new insights!

    Finally a String switch statement!! :)

    Eggsy

    • November 16, 2010 at 12:36 pm | #14

      Hi eggsy,

      Thanks for your valuable comment.Ya.Finally switch statement has string as case variable.

  9. November 16, 2010 at 11:39 am | #15

    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.

  10. Ramki
    November 16, 2010 at 12:12 pm | #16

    Its very good

    please post more about java 7..

    • November 16, 2010 at 12:32 pm | #17

      Hi Ram,

      Thanks for your valuable comments.Its increase my enthusiasm to know about java 7.Thanks.

  11. Miek
    November 16, 2010 at 1:07 pm | #18

    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.

  12. visitor
    November 16, 2010 at 2:42 pm | #19

    final int strHash = someString.hashCode();
    switch( strHash ){
    case “string in switch”.hashCode(): /*do something*/ break;

    }

    • November 16, 2010 at 3:56 pm | #20

      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!

  13. November 16, 2010 at 3:00 pm | #21

    Hi Munees,
    Excellent article. Keep up the good work!!
    - Gift Sam

  14. Faisal Feroz
    November 16, 2010 at 5:39 pm | #23

    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/

    • November 17, 2010 at 1:22 am | #24

      Hi Faisal,

      Thanks for your valid information.I don’t know,How can i miss that important feature.Anyway,Thanks for your sharing.

    • November 17, 2010 at 6:21 am | #25

      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.

  15. January 14, 2011 at 11:22 am | #26

    That’s an usefull article with good examples. Thanks.

  16. Billy Bob
    March 24, 2011 at 6:45 pm | #27

    Big deal. Java’s still slow.

  17. Mikis
    March 28, 2011 at 6:51 am | #28

    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.

  18. Aslam Mohammed
    July 7, 2011 at 8:51 am | #29

    Good article indeed!

  19. Krishna Chourasiya
    January 2, 2012 at 9:38 am | #30

    Good Article……

  20. February 13, 2012 at 4:19 pm | #31

    Reblogged this on Zeeshan Akhter.

  21. September 22, 2012 at 5:21 pm | #32

    Very nice and exciting. Thanks for sharing

  22. March 13, 2013 at 10:47 am | #33

    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

  1. December 19, 2010 at 6:07 am | #1
  2. March 28, 2011 at 4:52 pm | #2
  3. February 6, 2012 at 6:19 pm | #3
  4. February 7, 2012 at 11:01 am | #4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: