Introduction to Java 7 (Devoxx Nov/2011)

36
Slide Design by http://www.kerrykenneally.com The WGJD - Intro to Java 7 Ben Evans and Martijn Verburg (@kittylyst @karianna) http://www.teamsparq.net 1 Saturday, 24 December 11

description

Part 1/3 of our Devoxx University session. An introduction to some of the features of Java 7! We're still working out where to host the torrent of our Virtual Box image with the coding exercises, we'll update the description when we sort it out.

Transcript of Introduction to Java 7 (Devoxx Nov/2011)

Page 1: Introduction to Java 7 (Devoxx Nov/2011)

Slide Design by http://www.kerrykenneally.com

The WGJD - Intro to Java 7

Ben Evans and Martijn Verburg (@kittylyst @karianna)

http://www.teamsparq.net

1Saturday, 24 December 11

Page 2: Introduction to Java 7 (Devoxx Nov/2011)

This is not an Oracle legal slide

2

2Saturday, 24 December 11

Page 3: Introduction to Java 7 (Devoxx Nov/2011)

3

No, we’re not in sales!

• Directors of TeamSparq– “Optimisation for Technical Teams”– Ben is known for his performance & concurrency work– Martijn is the “Diabolical Developer” and Ben can be just as ‘evil’

• Authors of “The Well-Grounded Java Developer”

• Co-Leaders of the LJC (London’s Java User Group)– Hold a seat on the JCP SE/EE Executive Committee

• Regular conference speakers– JavaOne, Devoxx, OSCON, etc

3Saturday, 24 December 11

Page 4: Introduction to Java 7 (Devoxx Nov/2011)

4

Who are these two anyway?

4Saturday, 24 December 11

Page 5: Introduction to Java 7 (Devoxx Nov/2011)

5

How this university session is going to work

• This is a fact!– This is an opinion

• This session may be a little different to advertised...– Being a “Well-Grounded Java Developer” changes over time

• This session will go fairly quickly– But the slides will be made available– And you can always get hold of us for any questions

5Saturday, 24 December 11

Page 6: Introduction to Java 7 (Devoxx Nov/2011)

6

Timetable

• Introduction to Java 7 (~35 mins)

• BREAK (5 minutes)

• Polyglot and functional programming (~50 mins)

• BREAK (5 minutes)

• Modern Java Concurrency (~60 mins)

• Conclusion and Extra Q&A

6Saturday, 24 December 11

Page 7: Introduction to Java 7 (Devoxx Nov/2011)

Real Developers code through bathroom breaks

7

7Saturday, 24 December 11

Page 8: Introduction to Java 7 (Devoxx Nov/2011)

The Well-Grounded Java Developer...

• Is not just a Java language whizz– Understands the basics of the JVM– Understands software development is a social activity– Utilises software craftsmanship approaches– Understands architecture

• Is looking at Java 7•

• Is looking at polyglot and functional programming

• Is looking at modern concurrency practices

8

8Saturday, 24 December 11

Page 9: Introduction to Java 7 (Devoxx Nov/2011)

Java 7 - Why is it important

• The WGJD wants to code rapidly

• The WGJD wants to code concisely

• The WGJD wants to take advantage of:– The compiler (including JIT)– The JVM

• Java 7 gives you many improvements in these areas

9

9Saturday, 24 December 11

Page 10: Introduction to Java 7 (Devoxx Nov/2011)

10

OpenJDK - The Java 7/8 split

• 20 Sep 2010 - Mark Reinhold announces Plan B– Splits OpenJDK effort into Java 7 (July 2011) and Java 8 (2013)– Popular choice with the community

• July 2011 - Java 7 goes gold

• Some new features to be delayed until JDK 8– Closures Lambdas SAM literals– Modularisation (aka Jigsaw)

• JRockit features to be merged into OpenJDK– Enhanced management of the JVM– Ongoing merge of the VM engineering groups

10Saturday, 24 December 11

Page 11: Introduction to Java 7 (Devoxx Nov/2011)

11

Contents of JDK 7 Release

• Project Coin

• NIO.2

• Method Handles

• invokedynamic

• Concurrency Refresh

• Odds & Ends

11Saturday, 24 December 11

Page 12: Introduction to Java 7 (Devoxx Nov/2011)

12

Project Coin

• “Small” changes

• Language Level, not VM

• Stay away from the type system

• Developed in a very OSS manner– Was an ‘interesting’ experience for all involved

12Saturday, 24 December 11

Page 13: Introduction to Java 7 (Devoxx Nov/2011)

13

Project Coin - Highlights

• Strings in switch

• try-with-resources (aka ARM or TWR)

• Diamond Syntax

• Multicatch with precise rethrow

• Enhanced syntax for numeric literals

• Varargs / autoboxing warning

13Saturday, 24 December 11

Page 14: Introduction to Java 7 (Devoxx Nov/2011)

14

Strings in switch

• Code along exercise: FeedingSchedule.java

14Saturday, 24 December 11

Page 15: Introduction to Java 7 (Devoxx Nov/2011)

15

try-with-resources

15Saturday, 24 December 11

Page 16: Introduction to Java 7 (Devoxx Nov/2011)

16

Diamond syntax

• Code along exercise: HordeOfOtters.java

16Saturday, 24 December 11

Page 17: Introduction to Java 7 (Devoxx Nov/2011)

Personally I’d stick to Java 1.4

17

17Saturday, 24 December 11

Page 18: Introduction to Java 7 (Devoxx Nov/2011)

18

NIO.2 - New I/O version 2

• A new file system and path abstraction

• Based on Path– An abstract view of a ‘file like’ system

• Files class contains many helper methods, including– File manipulation (copy, move, rename etc)– Walking directory trees– Native file system support (e.g. symbolic links)

• Works with existing java.io.File code

18Saturday, 24 December 11

Page 19: Introduction to Java 7 (Devoxx Nov/2011)

19

NIO.2 - New I/O version 2

• Asynchronous (non-blocking) I/O

• For sockets and files

• Mainly utilises java.util.concurrent.Future

• New NetworkChannel– Socket/Channel construct– Binding, options and multicast

19Saturday, 24 December 11

Page 20: Introduction to Java 7 (Devoxx Nov/2011)

20

Path and Files - Some examples

• Code along exercise: Housekeeping.java

20Saturday, 24 December 11

Page 21: Introduction to Java 7 (Devoxx Nov/2011)

21

URL stream to file - Java 6 style NIO

21Saturday, 24 December 11

Page 22: Introduction to Java 7 (Devoxx Nov/2011)

22

URL stream to file in Java 7

• Code along exercise: VetNotes.java

22Saturday, 24 December 11

Page 23: Introduction to Java 7 (Devoxx Nov/2011)

23

NIO.2 - Future base file I/O

23Saturday, 24 December 11

Page 24: Introduction to Java 7 (Devoxx Nov/2011)

Monty Python eat your heart out

24

24Saturday, 24 December 11

Page 25: Introduction to Java 7 (Devoxx Nov/2011)

25

Method Handles

• We already have Reflection API– It’s all pretty horrible

• JDK 7 introduces a new way to inspect at runtime

• Method Handles– Represent the ability to call a method– Implement a subclass of java.lang.invoke.MethodHandle– Are strongly-typed and typesafe– Needed for invokedynamic - but also used standalone

25Saturday, 24 December 11

Page 26: Introduction to Java 7 (Devoxx Nov/2011)

26

Example - ThreadPoolManager

26Saturday, 24 December 11

Page 27: Introduction to Java 7 (Devoxx Nov/2011)

27

Cancelling using Reflection

27Saturday, 24 December 11

Page 28: Introduction to Java 7 (Devoxx Nov/2011)

28

Cancelling using MethodHandle

28Saturday, 24 December 11

Page 29: Introduction to Java 7 (Devoxx Nov/2011)

29

invokedynamic

• invokedynamic is a key new JVM feature– It’s the first new bytecode since Java 1.0– Joins invokevirtual, invokestatic, invokeinterface and

invokespecial

• Relaxes a key part of the static typing system– User code can determine dispatch at runtime

• Aims to be as fast as regular method dispatch– e.g. invokevirtual

• No Java syntax in Java 7– But maybe for Java 8

29Saturday, 24 December 11

Page 30: Introduction to Java 7 (Devoxx Nov/2011)

30

invokedynamic contd.

• JRuby, Jython, Groovy, Clojure et al all benefit– JRuby gains a lot, Clojure not so much– Even Scala is using it now!

• JRuby has been working closely with the JSR-292 team– Big wins– Makes JRuby enticing to Ruby developers

• Java 8 also gets a boost– Lambdas have a much nicer design with invokedynamic

30Saturday, 24 December 11

Page 31: Introduction to Java 7 (Devoxx Nov/2011)

Other JVM Languages?

Meh - Use the Golden Hammer!

31

31Saturday, 24 December 11

Page 32: Introduction to Java 7 (Devoxx Nov/2011)

32

Java 7 Concurrency Refresh

• Java 7 has a number of new concurrency toys

• The headline item is Fork / Join– similar to MapReduce, useful for a certain class of problems– fork and join executions are not necessarily threads

• We’ll cover this in the final hour!

32Saturday, 24 December 11

Page 33: Introduction to Java 7 (Devoxx Nov/2011)

33

Java 7 - That’s not all

• Nimbus Look & Feel for Swing

• New helper classes including Objects– .deepEquals() and friends

• Enhanced JDBC (including try-with-resources support)

• Better unicode support– Thanks in part to Tom Christiansen of Perl fame

• More small changes in niche areas

33Saturday, 24 December 11

Page 34: Introduction to Java 7 (Devoxx Nov/2011)

34

New Faces in the OpenJDK

Original Image by Acaben34Saturday, 24 December 11

Page 35: Introduction to Java 7 (Devoxx Nov/2011)

35

What We Didn’t Have Time To Talk About

• Java 8– Java FX 2.0

• Java EE 6/7

• Java ME– Android and _that_ lawsuit

• OpenJDK changes & JDK Enhancement Proposals (JEP)

• Java Community Process (JCP)– Java Specification Requests (JSRs)– Java User Groups (JUGs) and reinvigorating the Community

35Saturday, 24 December 11

Page 36: Introduction to Java 7 (Devoxx Nov/2011)

What? You still here?

Go take a break will you!

36

36Saturday, 24 December 11