Archive

Posts Tagged ‘java 7’

Java 7 Awesome Aspect-Automatic Resource Management

December 19, 2010 7 comments

Introduction:

In my previous article,” Java 7 Awesome Features” I had missed one of the greatest aspect of Java 7.That is nothing but “Automatic Resource Management”. This article deals with the same.

Managing Resource is one of the keen thing in development. Because if we don’t manage the resource effectively it will cause lot of issues like PermGen Space Error, TooManyConnections Error etc.

For avoid those problems Java 7 introduce the great aspect “Automatic Resource Management”. By this feature java can automatically manage the resources effectively. So java frees the developer from lots of overhead. Lets have a prolix look on this.

Syntax:


try(Resource 1;Resource 2;Resource 3….)

{

//block of statements

}

catch(Exception e)

{

//block of statements

}

Here is simple code snippet which shows you the typical try-catch-finally block. In the code we create the jdbc connection for access the data from database.


Connection connection = null;

Statement statement = null;

try

{

connection =    DriverManager.getConnection(“databaseurl”,”username(opt)”,”password(opt)”);

statement = connection.createStatemnet();

boolean executionStatus= statement.execute(“query”);

}

catch(Exception e)

{

//Block of code for handle the exception

e.printStacktrace();

}

finally

{

try

{

statement.close();

connection.close();

}

catch(Exception e)

{

//block of statements for handle exceptions.

}

}

In the above snippet, suppose we forget to close the statement and connection, what happened? May be after some operations (which also open the connection) “Too Many connection error” will occurred.

But java 7 keeps us in safe zone from those kinds of errors by mange the resource automatically. In java 7 we can write the above snippet like this,


Connection connection = null;

Statement statement = null;

try(connection =    DriverManager.getConnection(“databaseurl”,”username(opt)”,”password(opt)”);

statement = connection.createStatemnet())

{

boolean executionStatus= statement.execute(“query”);

}

catch(Exception e)

{

//block of statements for handles the exceptions

}

In the above snippet, there are no overheads like close the connections and statements properly. Java 7 can manage those things automatically for us.

Note:

Java 7 mange the resources which are sub interfaces and implementing classes of AutoCloseable.So we can pass the resources which are extends or implements the interface AutoCloseable.

That’s all folks, I think this article makes you bit more knowledgeable about java 7 awesome aspect “Automatic resource Management”, If you feel the same, please leave your footprints(comments) here. Joyous coding day…..

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.