Java7 Features

  • View
    171

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Text of Java7 Features

  • 1. Java SE 7 {finally}2011-08-18 Andreas Enbohm 2004 Capgemini - All rights reserved

2. Java SE 7A evolutionary evolement of Java 6 years since last update Some things left out, will briefly discuss this at the end Oracle really pushing Java forward - a lot political problems with Sun made Java 7 postphoned several times Java still growing (1.42%), #1 most used language according to TIOBE with 19.4% (Aug 2011) My top 10 new features (not ordered in any way )Sida 2 15 januari 2014 2009 Capgemini - All rights reserved 3. Java SE 7 Language ChangesNumber 1: Before :Sida 3 15 januari 2014 2009 Capgemini - All rights reservedTry-with-resources Statement (or ARM-blocks)static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException { BufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { if (br != null) { try { br.close(); } catch (IOException ignore){ //do nothing } } } } 4. Java SE 7 Language ChangesNumber 1:Try-with-resources Statement (or ARM-blocks)With Java 7: static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } }Sida 4 15 januari 2014 2009 Capgemini - All rights reserved 5. Java SE 7 Language ChangesNumber 1 - NOTE: The try-with-resources statement is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.Sida 5 15 januari 2014 2009 Capgemini - All rights reserved 6. Java SE 7 Language ChangesNumber 2:Strings in switch Statementspublic String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOfDay; switch (dayOfWeekArg) { case "Monday": typeOfDay = "Start of work week"; break; case "Tuesday": case "Wednesday": case "Thursday": typeOfDay = "Midweek"; break; case "Friday": typeOfDay = "End of work week"; break; case "Saturday": case "Sunday": typeOfDay = "Weekend"; break; default: throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg); } return typeOfDay; } Sida 6 15 januari 2014 2009 Capgemini - All rights reserved 7. Java SE 7 Language ChangesNumber 2 - NOTE: The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.Sida 7 15 januari 2014 2009 Capgemini - All rights reserved 8. Java SE 7 Language ChangesNumber 3: Before :Catching Multiple Exception Typestry { } catch (IOException ex) { logger.log(ex); throw ex; } catch (SQLException ex) { logger.log(ex); throw ex; }Difficult to eliminate code duplication due to different exceptions! Sida 8 15 januari 2014 2009 Capgemini - All rights reserved 9. Java SE 7 Language ChangesNumber 3:Catching Multiple Exception TypesWith Java 7 : try { } catch (IOException|SQLException ex) { logger.log(ex); throw ex; }Sida 9 15 januari 2014 2009 Capgemini - All rights reserved 10. Java SE 7 Language ChangesNumber 3 - NOTE: Catching Multiple Exception Types Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each.Sida 10 15 januari 2014 2009 Capgemini - All rights reserved 11. Java SE 7 Language ChangesNumber 4:Type Inference for Generic Instance CreationBefore: Map> myMap = new HashMap>();How many times have you sworn about this duplicated code? Sida 11 15 januari 2014 2009 Capgemini - All rights reserved 12. Java SE 7 Language ChangesNumber 4:Type Inference for Generic Instance CreationWith Java 7: Map> myMap = new HashMap();Sida 12 15 januari 2014 2009 Capgemini - All rights reserved 13. Java SE 7 Language ChangesNumber 4 - NOTE: Type Inference for Generic Instance Creation Writing new HashMap() (without diamond operator) will still use the raw type of HashMap (compiler warning)Sida 13 15 januari 2014 2009 Capgemini - All rights reserved 14. Java SE 7 Language ChangesNumber 5:Underscores in Numeric Literalslong creditCardNumber = 1234_5678_9012_3456L; long socialSecurityNumber = 1977_05_18_3312L; float pi = 3.14_15F; long hexBytes = 0xFF_EC_DE_5E; long hexWords = 0xCAFE_BABE; long maxLong = 0x7fff_ffff_ffff_ffffL; long bytes = 0b11010010_01101001_10010100_10010010;Sida 14 15 januari 2014 2009 Capgemini - All rights reserved 15. Java SE 7 Language ChangesNumber 5 - NOTE: Underscores in Numeric Literals You can place underscores only between digits; you cannot place underscores in the following places:At the beginning or end of a number Adjacent to a decimal point in a floating point literal Prior to an F or L suffixIn positions where a string of digits is expectedSida 15 15 januari 2014 2009 Capgemini - All rights reserved 16. Java SE 7 Concurrent UtilitiesNumber 6:Fork/Join Framework (JSR 166) a lightweight fork/join framework with flexible and reusable synchronization barriers, transfer queues, concurrent linked double-ended queues, and thread-local pseudo-random-number generators.Sida 16 15 januari 2014 2009 Capgemini - All rights reserved 17. Java SE 7 Concurrent UtilitiesNumber 6:Fork/Join Framework (JSR 166)if (my portion of the work is small enough) do the work directly else split my work into two pieces invoke the two pieces and wait for the resultsSida 17 15 januari 2014 2009 Capgemini - All rights reserved 18. Java SE 7 Concurrent UtilitiesNumber 6:Sida 18 15 januari 2014 2009 Capgemini - All rights reservedFork/Join Framework (JSR 166) 19. Java SE 7 Concurrent UtilitiesNumber 6:Fork/Join Framework (JSR 166)New Classes ForkJoinTaskRecursiveTaskRecursiveActionThreadLocalRandomForkJoinPoolSida 19 15 januari 2014 2009 Capgemini - All rights reserved 20. Java SE 7 Filesystem APINumber 7:NIO 2 Filesystem API (Non-Blocking I/O)Better supports for accessing file systems such and support for custom file systems (e.g. cloud file systems)Access to metadata such as file permissions More effecient support when copy/moving files Enchanced Exceptions when working on files, i.e. file.delete() now throws IOException (not just Exception)Sida 20 15 januari 2014 2009 Capgemini - All rights reserved 21. Java SE 7 JVM EnhancementNumber 8:Invoke Dynamic (JSR292)Support for dynamic languages so their performance levels is near to that of the Java language itselfAt byte code level this means a new operand (instruction) called invokedynamic Make is possible to do efficient method invocation for dynamic languages (such as JRuby) instead of statically (like Java) . Huge performance gainSida 21 15 januari 2014 2009 Capgemini - All rights reserved 22. Java SE 7 JVM EnhancementNumber 9:G1 and JVM optimizationG1 more predictable and uses multiple cores better than CMS Tiered Compilation Both client and server JIT compilers are used during starup NUMA optimization - Parallel Scavenger garbage collector has been extended to take advantage of machines with NUMA (~35% performance gain) Escape Analysis - analyze the scope of a new object's and decide whether to allocate it on the Java heap Sida 22 15 januari 2014 2009 Capgemini - All rights reserved 23. Java SE 7 JVM EnhancementNumber 9:Escape Analysispublic class Person { private String name; private int age; public Person(String personName, int personAge) { name = personName; age = personAge; } public Person(Person p) { this(p.getName(), p.getAge()); } } public class Employee { private Person person; // makes a defensive copy to protect against modifications by caller public Person getPerson() { return new Person(person) }; public void printEmployeeDetail(Employee emp) { Person person = emp.getPerson(); // this caller does not modify the object, so defensive copy was unnecessary System.out.println ("Employee's name: " + person.getName() + "; age: " + person.getAge()); } } Sida 23 15 januari 2014 2009 Capgemini - All rights reserved 24. Java SE 7 - Networking Number 10: Support for SDP Socket Direct Protocol (SDP) enables JVMs to use Remote Direct Memory Access (RDMA). RDMA enables moving data directly from the memory of one computer to another computer, bypassing the operating system of both computers and resulting in significant performance gains. The result is High throughput and Low latency (minimal delay between processing input and providing output) such as you would expect in a real-time application.Sida 24 15 januari 2014 2009 Capgemini - All rights reserved 25. Java SE 7 - NetworkingNumber 10 - NOTE: Support for SDP The Sockets Direct Protocol (SDP) is a networking protocol developed to support stream connections over InfiniBand fabric.Solaris 10 5/08 has support for InfiniBand fabric (which enables RDMA). On Linux, the InfiniBand package is called OFED (OpenFabrics Enterprise Distribution).Sida 25 15 januari 2014 2009 Capgemini - All rights reserved 26. Java SE 7A complete list of all new features can be seen on http://www.oracle.com/technetwork/java/javase/jdk7-relnotes418459.htmlSida 26 15 januari 2014 2009 Capgemini - All rights reserved 27. Java SE 8 Some features where left out in Java 7; most important are Project Lambda (closures) and Project Jigsaw (modules) targeted for Java 8 (late 2012) Lambdas (and extension methods) will probably be the biggest single change ever made on the JVM. Will introduce a powerful programming model, however it comes with a great deal of complexity as well. Modules will made it easier to version modules (no more JAR-hell) and introduce a new way of defining classpaths Sida 27 15 januari 2014 2009 Capgemini - All rights reserved 28. Java SE 7 & 8Questions?Sida 28 15 januari 2014 2009 Capgemini - All rights reserved