Archive

Posts Tagged ‘project coin’

Java 7 awesome Features

November 15, 2010 38 comments

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.