Java 7 was released way back in 2011, but it's unfortunately not THAT uncommon for a large organization to get set in its ways (having found years ago, those battle-tested, magical, set of exact JVM tuning arguments that work for *their particular* application stack on a certain version of a 1.6 JVM) so that you don't get exposed to "newer" features. And to only get dragged kicking and screaming into JDK8 when Oracle deprecated certain versions.
So, what did Java 7 bring us? As developers, it seemed to be mostly about cleaning up syntax/getting rid of some of the boilerplate...
- Strings in switch statements - sure, other languages had it for years, but you weren't able to switch on Strings until Java 7. Nice to have, not especially mind-blowing.
- try with resources - This was a welcome addition - no longer did you have the super-awkward "declare your variable outside the block just so you have access to it in a second try/catch so you can close it..." e.g.
Connection conn = null; try{ conn = getConnectionFromSomewhere(); // do work with the resource } catch (Exception e){ // handle } finally { if (conn != null){ try { conn.close(); } catch (Exception someOtherExceptionYouProbablyIgnore) { // yah, you probably ignore this } } }
Nope, now you can just dotry(Connection conn = getConnectionFromSomewhere();){ // do work with the resource } catch (Exception e) { // handle }
You can also have your own objects work this way, look into java.lang.AutoClosable - Multi-catch - Another nice little cleanup, rather than listing out all the exceptions you want to handle (even if you handle some of them the same), you can now use the | pipe operator to union the exceptions you are handling. Instead of this:
} catch (IOException ex) { logger.log(ex); throw ex; } catch (SQLException ex) { logger.log(ex); throw ex; }
write this:} catch (IOException|SQLException ex) { logger.log(ex); throw ex; }
- Binary literals - "0b" prefix, e.g. 0b10100111001 is 1337
- left-to-right Improved type inference (<>) Removed the necessity of writing tripe like:
Map<String, List<Integer>> m = new HashMap<String, List<Integer>>();
simplifying it slightly to:Map<String, List<Integer>> m = new HashMap<>();
(This seems slightly backwards to me, we're essentially specifying the details on the interface and waving our hands over the concrete implementation, instead ofMap<> m = new HashMap<String, List<Integer>>(); // does not work
but I'm not the designer - you must use the first version =) - Underscores in numeric literals What's easier to understand at a glance? Counting the zeros in 1000000000 or 1_000_000_000?
- A whole slew of new APIs for NIO - notably, java.nio.Path and the WatchService, which allows you to be notified of changes to a path you're watching (there are a ton of applications for this)
Those are the big ones in my opinion. Did I miss any of your favorites?
No comments:
Post a Comment