Java7 Features

28
© 2004 Capgemini - All rights reserved Java SE 7 {finally} 2011-08-18 Andreas Enbohm

description

 

Transcript of Java7 Features

Page 1: Java7 Features

© 2004 Capgemini - All rights reserved

Java SE 7 {finally}

2011-08-18

Andreas Enbohm

Page 2: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023Sida 2

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 )

Page 3: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 1: Try-with-resources Statement (or ARM-blocks)

Before :

Sida 3

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 } } }}

Page 4: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 1: Try-with-resources Statement (or ARM-blocks)

With Java 7:

Sida 4

static String readFirstLineFromFile(String path) throws IOException { try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); } }

Page 5: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 6: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 2: Strings in switch Statements

Sida 6

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;}

Page 7: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 8: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 3: Catching Multiple Exception Types

Before :

Difficult to eliminate code duplication due to different exceptions!

Sida 8

try { …} catch (IOException ex) { logger.log(ex); throw ex; } catch (SQLException ex) { logger.log(ex); throw ex; }

Page 9: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 3: Catching Multiple Exception Types

With Java 7 :

Sida 9

try { …} catch (IOException|SQLException ex) { logger.log(ex); throw ex; }

Page 10: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 11: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 4: Type Inference for Generic Instance Creation

Before:

How many times have you sworn about this duplicated code?

Sida 11

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

Page 12: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 4: Type Inference for Generic Instance Creation

With Java 7:

Sida 12

Map<String, List<String>> myMap = new HashMap<>();

Page 13: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 14: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Language Changes

Number 5: Underscores in Numeric Literals

Sida 14

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;

Page 15: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 16: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 17: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Concurrent Utilities

Number 6: Fork/Join Framework (JSR 166)

Sida 17

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

Page 18: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Concurrent Utilities

Number 6: Fork/Join Framework (JSR 166)

Sida 18

Page 19: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – Concurrent Utilities

Number 6: Fork/Join Framework (JSR 166)

New Classes

• ForkJoinTask

• RecursiveTask

• RecursiveAction

• ThreadLocalRandom

• ForkJoinPool

Sida 19

Page 20: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 21: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 22: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 23: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 – JVM Enhancement

Number 9: Escape Analysis

Sida 23

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()); } }

Page 24: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 25: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 26: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7

A complete list of all new features can be seen on http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html

Sida 26

Page 27: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

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

Page 28: Java7 Features

© 2009 Capgemini - All rights reserved10 april 2023

Java SE 7 & 8

Questions?

Sida 28