• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java7 Features
 

Java7 Features

on

  • 179 views

 

Statistics

Views

Total Views
179
Views on SlideShare
179
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java7 Features Java7 Features Presentation Transcript

    • Java SE 7 {finally} 2011-08-18 Andreas Enbohm © 2004 Capgemini - All rights reserved
    • Java SE 7 A 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
    • Java SE 7 – Language Changes Number 1: Before : Sida 3 15 januari 2014 © 2009 Capgemini - All rights reserved Try-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 } } } }
    • Java SE 7 – Language Changes Number 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
    • Java SE 7 – Language Changes Number 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
    • Java SE 7 – Language Changes Number 2: Strings in switch Statements public 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
    • Java SE 7 – Language Changes Number 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
    • Java SE 7 – Language Changes Number 3: Before : Catching Multiple Exception Types try { … } 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
    • Java SE 7 – Language Changes Number 3: Catching Multiple Exception Types With Java 7 : try { … } catch (IOException|SQLException ex) { logger.log(ex); throw ex; } Sida 9 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – Language Changes Number 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
    • Java SE 7 – Language Changes Number 4: Type Inference for Generic Instance Creation Before: Map<String, List<String>> myMap = new HashMap<String, List<String>>(); How many times have you sworn about this duplicated code?  Sida 11 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – Language Changes Number 4: Type Inference for Generic Instance Creation With Java 7: Map<String, List<String>> myMap = new HashMap<>(); Sida 12 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – Language Changes Number 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
    • Java SE 7 – Language Changes Number 5: Underscores in Numeric Literals long 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
    • Java SE 7 – Language Changes Number 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 suffix In positions where a string of digits is expected Sida 15 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – Concurrent Utilities Number 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
    • Java SE 7 – Concurrent Utilities Number 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 results Sida 17 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – Concurrent Utilities Number 6: Sida 18 15 januari 2014 © 2009 Capgemini - All rights reserved Fork/Join Framework (JSR 166)
    • Java SE 7 – Concurrent Utilities Number 6: Fork/Join Framework (JSR 166) New Classes • ForkJoinTask • RecursiveTask • RecursiveAction • ThreadLocalRandom • ForkJoinPool Sida 19 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – Filesystem API Number 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
    • Java SE 7 – JVM Enhancement Number 8: Invoke Dynamic (JSR292) Support for dynamic languages so their performance levels is near to that of the Java language itself At 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 gain Sida 21 15 januari 2014 © 2009 Capgemini - All rights reserved
    • Java SE 7 – JVM Enhancement Number 9: G1 and JVM optimization G1 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
    • Java SE 7 – JVM Enhancement Number 9: Escape Analysis public 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
    • 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
    • Java SE 7 - Networking Number 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
    • Java SE 7 A complete list of all new features can be seen on http://www.oracle.com/technetwork/java/javase/jdk7-relnotes418459.html Sida 26 15 januari 2014 © 2009 Capgemini - All rights reserved
    • 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
    • Java SE 7 & 8 Questions? Sida 28 15 januari 2014 © 2009 Capgemini - All rights reserved