- JDK 1.1 (Feb 1997)
- J2SE 1.2 (Dec '98)
- J2SE 1.3 (May '00)
- J2SE 1.4 (Feb '02)
- J2SE 5.0 (sept 04)
- Java SE 6 (Dec '06)
Language changes is the area that I find most interesting because it is what developers will get to use in their day to day jobs. It's what people will notice. It's what people will talk about. Now, in Java SE 7 the language development was under a sub-project known as Project Coin. There was an open call for proposals between Feb 27th, 2009 and March 30, 2009 and in total 70 proposals received some extensive discussion. The 70 were then narrowed down to five or so and now we're going to take a look at the ones that are part of Java SE 7.
Binary Literals
In Java SE 7, the integral types (byte
, short
, int
, and long
) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b
or 0B
to the number. Type - inference for Generics
JDK 1.5 saw the introduction of generics. One of the great advantages of generics was cleaner code because the need for casting was eliminated (if the generics feature was used correctly!). However, unless you enjoyed those annoying compile time warnings about type safety you had to generify both the declaration and instantiation. For example, suppose you had a list of tennis balls... The annoying thing about having to do this is that it re-introduced code bloat - something generics was trying to reduce. Now in Java SE 7 there is type inference for generic instance creation. You can substitute the parameterized type of the constructor with an empty set of type parameters <>
(also known as the diamond operator). That means you can do: and you won't get any annoying warning but you'll still get all the benefits of compile time type safety provided by generics.
Catching multiple exceptions I really like this one. Say you're catching multiple exceptions where some of the exceptions don't form an inheritance hierarchy but share the same exception handling code. You're going to end up with code bloat. For example: With Java SE 7, you can now do: Not bad eh - bound to tidy up tonnes of code. In addition to the ability to catch mulitple exceptions there is also some new precision available regarding re-throwing of exceptions.
Underscores in literals Underscores can be used in literals now to make them more easier to read:
Strings can be used in Switch statements Switch statements are useful. They can be used with primitive types and enumerated types. In Java SE 7, they can also be used with Strings. Note: this simple but useful feature was officially suggested from the lead engineer of Project Coin, Joseph D. Darcy. You can check out his blog here.
Autoclosing of resources (also known as try-with-resources)
This is a classic programming error. You use a resource (e.g. a JDBC connection) and you forget to close it. Now, once the resource implements java.io.AutoClosable
(or the interface java.io.Closeable
) you don't need to close it. Yippee. The list of classes in the JDK that implement this in the JDK is pretty extensive. Just check the JDoc. for the AutoCloseable interface. Interestingly this proposal came from Joshua Bloch, the acclaimed author of the stupendous Effective Java - a must read for all serious programmers. Note in addition to the new progamming features, Java SE 7 also includes:
- Increased support for dynamic languages (such as Ruby and Python)
- Improved security support. Eliptic-curve crytography (ECC) implementation will be included.
- Improved class loader support
- and much more
Check it out...
References