Join David Gassner for an in-depth discussion in this video An overview of Java 7 new features, part of Advanced Java Programming.
There are many parts of this video series where I describe new features of Java 7. In this chapter I focus on a few small improvements, but other improvements are described in other parts of the course. Here are some of the new features of the Java programming language that you might find helpful. Some of these new features are little simplifications. For example, the diamond operator is used in collections and other data types that can contain other objects to indicate the data types of those objects.
In previous versions of Java, you had to declare the data type of the contained objects twice, once in the declaration of the collection, for example, in this array list, and once in the call to the constructor method. But that was redundant. You shouldn't have to do it twice, and in Java 7, you only have to declare the data type of the contained class once. So the new syntax is shown below. It's a small change, but it will save some coding time. Here's another nice simplification. If you like using the Try/catch architecture to wrap around objects that have to be closed when you're done with them, this kind of code might look familiar.
In previous versions of Java, if you were using an object such as a file stream, you would first declare the object, then you would instantiate and use it within a Try clause, you'd catch an exception, and then you might close it within Finally clause. And the only reasons you have declared it above the Try/catch was so it would be visible within all of those code blocks. In Java 7, there's a new syntax called TryWithResources. You move the declaration and instantiation of the object inside a pair of parentheses right after the Try keyword.
And before the Try keywords code block, then you do whatever you need to do and catch any exceptions. And when the entire Try/catch Block is complete, any objects that you declared inside those parentheses will be automatically closed for you. Again, a simple improvement in the language that saves quite a bit of code. And here's one that doesn't so much save code as save your eyeballs. Numeric literals can now include underscores to make them easier to read. These two bits of code are identical functionally.
They both declare an integer variable of one million. But in the second version, I've inserted underscores where you might put commas or other thousand separators depending on your nationality and language. This makes the code easier to read later on. Again, it doesn't change its functionality. A million is still a million, and it's still just an integer. But it makes it easier to read and will save some time and aggravation. And there are other useful features in Java 7. The ability to use string values in switch statements, a whole new file system API that makes it much easier to read, write, and manage files, and new support for dynamic languages.
The new features of Java 7 are only available in the Java 7 Developers Kit. They're not available, for example, in the version of Java that's used with the Android Software Developers Kit. They might come to that version of Java later. But for now, if you want to use these features, you'll need to use the Java 7 Developers Kit that comes from Oracle.
- Installing Java on Windows and Mac OS X
- Installing Eclipse
- Using new features such as simplified generics
- Working with advanced class structures (member, local inner, etc.)
- Using the Reflection API
- Navigating inheritance trees
- Managing unordered and ordered sets
- Peeking and polling with queues
- Testing and error handling
- Managing files and directories
- Working with I/O streams
- Next steps with Java