Java 1.7 - What's new? Release date, code examples and performance
[10 Apr 10 at 09:17:26 - 53 comment(s)]
I have been reading quite a lot about Java 1.7. There are articles about what's new, some code examples, some benchmark to compare performance with previous version of Java and discussion on when it will be released. I have decided to regroup all I have discovered in this article so that I and maybe you, won't have to spend hours surfing the web to find all this information. Don't hesitate to leave a comment if I missed something.What's new in Java 1.7?
First thing first. To determine what set of small language changes should be added to JDK 7, a project has been set up called Project Coin. The final five changes (bit more than 5) are described on the following blog entry of Joseph D. Darcy.So what made it through is the following:
- Strings in switch
- Automatic Resource Management
- Improved Type Inference for Generic Instance Creation (diamond)
- Simplified Varargs Method Invocation
- An omnibus proposal for better integral literals
- Language support for Collections
- Language support for JSR 292
These features are divided in different categories:
VM
- Compressed 64-bit object pointers
- Garbage-First GC (G1)
- JSR 292: VM support for non-Java languages (InvokeDynamic)
lang
- SR 294: Language and VM support for modular programming
- JSR 308: Annotations on Java types
- JSR TBD: Small language enhancements (the Project Coin I was talking about)
- JSR TBD: Project Lambda
core
- Modularization (Project Jigsaw)
- Upgrade class-loader architecture
- Method to close a URLClassLoader
- Unicode 5.1
- Concurrency and collections updates (jsr166y)
- JSR 203: More new I/O APIs for the Java platform (NIO.2)
- SCTP (Stream Control Transmission Protocol)
- SDP (Sockets Direct Protocol)
- Elliptic-curve cryptography (ECC)
client
- XRender pipeline for Java 2D
- Forward-port 6u10 deployment features
- Create new platform APIs for 6u10 graphics features
- Nimbus look-and-feel for Swing
- Swing JLayer component
web
- Update the XML stack
I think that's it for the new features. Maybe it's a good idea to see some code examples now.
Code examples for new features in Java 1.7
Most of what is below come from the excellent article from Joe Wright on his blog about New language features in Java 7Language support for collections
This is all about writing less code when you create a List, a Set or a Map. You don't have to instantiate the Object and then add the element to the Collection. You can now do it in 1 line. List list = ["item"];
String item = list[0];
Set set = {"item"};
Map map = {"key" : 1};
int value = map["key"];
String item = list[0];
Set
Map
int value = map["key"];
Automatic Resource Management
Annoyed to have verbose code because of try / catch statement. You will love this one.Indeed, this:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
Become this:try {
return br.readLine();
} finally {
br.close();
}
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
}
return br.readLine();
}
Improved Type Inference for Generic Instance Creation (diamond)
Same thing, today you specify the Generic when you declare the interface of your object and then you have to repeat yourself when you instantiate the object. You won't have to now as you can do: Map> map = new HashMap<>();
Underscores in numeric literals
Not sure this one will be useful to lot of people. You can do: int billion = 1_000_000_000;
Strings in switch
Nothing to explain here, the title says it all. String availability = "available";
switch(availability) {
case "available":
//code
break;
case "unavailable":
//code
break;
case "merged":
//code
default:
//code
break;
}
switch(availability) {
case "available":
//code
break;
case "unavailable":
//code
break;
case "merged":
//code
default:
//code
break;
}
Binary literals
You can create binary literals using the 0b prefix int binary = 0b1001_1001;
That's it for the code examples. It would be great if someone can point me on more things. I am sure there are plenty of other cool stuff.
Performance of Java 1.7
I have picked up the tests I ran from an article from Taranfx posted here.So the tests are the following (my code, not his but I followed the same ideas). I ran all of this on a Macbook Pro running ArchLinux (the one with an Intel(R) Core(TM)2 Duo CPU T7700 @ 2.40GHz. I think 2 years old). I have 2Gb of RAM. I set the Heap Size to 728m (-Xms728m -Xmx728m).
Test 1. Add 1 Million String values to a List (the String is a UUID generated using UUID.randomUUID()).
Test 2. HashMap with 1 million keys, values. Each key, value pair is being calculated via concurrent thread. The key is a UUID and the int is generated using Math.random()
Test 3. Printing 1 million items of ArrayList to number of Files (1000). Writing to different files happening in parallel.
I am just comparing Java 1.6 (1.6.0_19) vs Java 1.7 (b87). I added Java 1.5 to this benchmark following a request in the comments but not Java 1.4 as it is from an other age to me.
So here is the result
Java 1.5 | Java 1.6 | Java 1.7 | |
Test 1 | 10,698 sec | 9,481 sec | 9,328 sec |
Test 2 | 69,827 sec | 37,935 sec | 36,636 sec |
Test 3 | 26,931 sec | 30,868 sec | 27,383 sec |
Release date of Java 1.7
In November 2009, it was planned to be in September 2010 as there would be 3 more milestones. Milestone 6 was completed with build 84. b85 was scheduled for the 4th of March 2010, first build of Milestone 7. b87 used in this article has been released the 25th of March 2010. So it looks like a release for September 2010 is more than likely. Though I have been reading an interesting subject on the stackoverflow forum here. You can check the blog of Alex Miller as well. He publishes links to all blog and news items referring to the progress of Java7What are the new features in JDBC 4.1 of Java 1.7?
JDBC 4.1 becomes part of Java 1.7 / Java SE 7. The upcoming JDBC 4.1 will have two new features when compared to JDBC 4.0 and earlier versions. They are:Usage of try-with-resources Statement: In JDBC 4.1, the statement try-with-resources can be used to close the JDBC resources automatically. Using this try-with-resources, the objects of type java.sql.connection, java.sql.ResultSet and java.sql.statement can be automatically closed. Example of try-with-resources in JDBC 4.1 is:
public static void sampleQueryProcessing(Connection sampleCon) throws SQLException { String sampleQuery = "select ROLLNO, NAME, ADDRESS from STUDENT";
try (Statement sampleStmt = sampleCon.createStatement())
{
ResultSet sampleResultSet = sampleStmt.executeQuery(sampleQuery);
while (rs.next())
{
int rollNo = sampleResultSet.getInt("ROLLNO");
String studentName = sampleResultSet.getString("NAME");
String studentAddress = sampleResultSet.getString("ADDRESS");
System.out.println(“ROLLNO:” + rollNo + " NAME: " + studentName + "
ADDRESS: " + studentAddress);
}
}
}
The try statement “try (Statement sampleStmt = sampleCon.createStatement())” is an example of the try-with-resources statement in JDBC 4.1. In this line of code, a new Statement instance called sampleStmt is created within the try statement. When the try block terminates, the object sampleStmt will be automatically closed.
RowSetFactoryInterface and RowSetProviderClass: The JDBC 4.1 belonging to Java 1.7 / Java 7 introduces a new interface called RowSetFactoryInterface and a new class called RowSetProvider class using which any types of row sets that are supported by the JDBC Driver can be created. Example of RowSetFactory and RowSetProvider is:
public void sampleMethod(String sampleUserName, String samplePassword) throws SQLException {
RowSetFactory sampleRowSetFactory = null;
JdbcRowSet sampleRowSet = null;
try
{
sampleRowSetFactory = RowSetProvider.newFactory();
sampleRowSet = sampleRowSetFactory.createJdbcRowSet();
sampleRowSet.setUrl("jdbc:sampleDriver:sampleAttribute");
sampleRowSet.setUsername(sampleUserName);
sampleRowSet.setPassword(samplePassword);
sampleRowSet.setCommand(""select ROLLNO, NAME, ADDRESS
from STUDENT");
sampleRowSet.execute();
// …
}
catch(Exception e)
{
}
}
sampleRowSetFactory = RowSetProvider.newFactory();
When Java 1.7 is released, these two features will be extensively used in JDBC 4.1
No comments:
Post a Comment