The 8 Nights of Java – Night 7

Our 8 Nights of Java series is nearly at an end! Tonight we look at Java 7, the first version of Java released entirely by Oracle. Java 7 included many goodies for developers, in the form of Project Coin. In a lot of ways, Project Coin was straight off of most developers Top 10 Wish List. Like Java 5, many of the futures were compile-time enhancements and helped to simplify numerous lines of code down to just one, allowing developers to focus on the important logic in their classes rather than syntax.

Jump to: [Night 1 | Night 2 | Night 3 | Night 4 | Night 5 | Night 6 | Night 7 | Night 8]

Java 7 Notable Features
Oracle released Java 7 (codename Dolphin) on July 7, 2011. After 5 years of Java 6 (including 131 updates) and the acquisition of Sun by Oracle, it was finally time to add some new features to Java. Key new functionality included:

  • Project Coin:
    • Strings in switch statements
    • Underscores (_) in numbers
    • Diamond (<>) operator
    • try-with-resources statement
    • Multi-catch blocks
    • Binary literals
  • NIO.2
  • Expanded Concurrency API

From Scott:

I love Java 7. While Java 5 “fixed” Collections by adding generics/autoboxing/foreach loops, Java 7 greatly improved the language by adding things that I didn’t even realize I was missing. The greatest of those (for a database-driven developer such as myself) was the try-with-resources statement. We’ve spoken in the past about finally closing JDBC resources, and to be honest, even we forget to do them sometimes. Could you blame us? The syntax was horrible. Especially because close() throws a checked exception which often must be caught inside another catch block! The try-with-resources statement fixed this not only by designing a syntax that was simple and easy to use, but finally providing a “standard” for all developers to work off of. After Java 7 was released, if I came across another developer opening/closing a connection without a try-with-resources statement, I could instruct them to replace it with one that does, thereby ensuring any possible resource leak is closed.

Project Coin had many other, very useful features. Strings in switch statements had been requested by numerous developers, especially beginners learning Java, for the better part of a decade. The diamond operator, while seemingly simply, greatly reduces assignments involving embedded Collections… such as “new HashMap<String,List<Integer>>” to just “new HashMap<>”. It was a pain to write out the full generic expression on both sides of an assignment! I don’t tend to use binary literatals or underscores in numbers, but I’m thrilled they were added to Java 7 since it makes reading other developers code a lot easier.

So many awesome features for developers in Java, I haven’t even gotten to the improved Concurrency API and new NIO.2. Both are extremely powerful, fully developed libraries that you can use to developer complex applications in minutes. And both got even better in Java 8 with the inclusion of… oops! Guess we’ll have to wait till tomorrow night to finish that sentence!

From Jeanne:

When I was young, the local college built a science building and named it the “New Science Building.” It’s still called that.The problem is what you call the one after that. NIO.2 had the same problem. “New I/O” didn’t take so well and now the name is taken. Luckily NIO.2 is nice! I use it a lot in my coding because I do a lot of file system work. I find myself using Apache Commons for I/O operations very rarely now.

Then there is Project Coin. While some is just semantic sugar like the diamond operator, it still makes the code cleaner and less redundant. The try with resources is great for not having to deal with boilerplate code! I also appreciate multi-catch. Before it existed, I’d often revert to “throws Exception” rather than independently catch two or three exceptions and handle them the same way. This happened a lot with a framework we used that declared two checked exceptions. It also happened a lot with XML parsing and reflection. With multi-catch, I’m more inclined to do the right thing and handle them all in one line.

The 8 Nights of Java – Night 4

Continuing The 8 Nights of Java series, tonight we focus on one of the single most important releases of Java. Java 1.4 was released a time when many businesses were starting to look to Java as a foundation for their software systems. After years of licensing proprietary or difficult to use software, Java was seen as a breadth of fresh air for many software engineers. It was helped, in part, by the decline of Windows-based computers and explosive growth of Mac and open-source Linux systems in the workplace and in homes. After all, if all of your developers are using different operating systems, then you need a software development platform that works on all of them and in that, Java was a success. So many business adopted Java 1.4 during this time and stayed on Java 1.4 for over a decade. In fact, many large enterprise systems still rely on Java 1.4 to this day. Hopefully, someone will be hired to update them soon!

Jump to: [Night 1 | Night 2 | Night 3 | Night 4 | Night 5 | Night 6 | Night 7 | Night 8]

Java 1.4 Notable Features
Sun released Java 1.4 (codename Merlin) on February 6th, 2002. Key new functionality included:

  • Regular expressions
  • Assertions
  • NIO Version 1
  • XML/XSLT support

From Jeanne:

I love regular expressions. They are one of my favorite language features because they are concise and expressive when written well. I was excited when they came out. While I started programming as a full time job after Java 1.4 was released, we were still using 1.3 as we waited for the application servers to support Java 1.4. This meant I was already employed and got to teach my teammates about regular expressions. I’ve actually given that presentation a number of times since.

I don’t use assertions because I write a lot of unit tests and the unit tests tell me about the type of problem that an assertion would. Tests help me design my code in a way that I don’t need assertions to tell me about the state of affairs. And then there is poor New I/O. I really like Java 7 NIO.2. New I/O “1”, not so much. It served it’s purpose in getting us to NIO.2 though.

From Scott:

I started programming professionally around the time that XML/XSLT were seen as the “new hot technology” to use on build enterprise systems. Having built-in support for XML transformations made Java look cutting edge at the time. While a lot of what is now done with XML is instead done by JSON, XML is still the core of many data-based systems. In fact, numerous web and mobile frond-end languages still use XML for their layouts, even if the developers using them rely solely on a GUI-based editor. Either way, Java 1.4 demonstrated that new technologies could be integrated into the JVM quite rapidly. That said, I’m still waiting for a JSON parser to become part of the standard Java runtime environment!

Java 1.4 also introduced NIO version 1, or NIO.1 for short. While NIO.2 is a quite powerful, if not commonly used framework, NIO.1 is basically dead weight at this point. The NIO.1 API never really caught on and today, very few people rely on file channels and the like. Since a key part of Java is keeping backwards compatibility, it remains part of the JRE, albeit rarely used.