90
Tuesday, July 12, 2011

Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Tuesday, July 12, 2011

Page 2: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

<Insert Picture Here>

Java SE 7: The Platform EvolvesDanny CowardPrincipal Engineer and Java Evangelist

Tuesday, July 12, 2011

Page 3: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

3

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

Tuesday, July 12, 2011

Page 4: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Oracle Strategy

• Deliver a complete, open, integrated stack of hardware, infrastructure, database, middleware, and business applications

• Exploit processor, systems, storage, and networking trends to deliver breakthrough innovations by combining Oracle software with Sun hardware

• Integrate components of Oracle’s software stack to provide unique value to customers

Tuesday, July 12, 2011

Page 5: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Middleware and Java in Oracle’s Strategy

• Comprehensive foundation for building and running custom and packaged applications

• Extremely well integrated• Industry-leading reliability and performance• Unified development and management• Basis for Oracle Fusion Applications

• Built with and for Java technology

Tuesday, July 12, 2011

Page 6: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Priorities for the Java Platforms

Grow Developer Base

Grow Adoption

Increase Competitiveness

Adapt to change

Tuesday, July 12, 2011

Page 7: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

JCP Reforms

• Developers’ voice in the Executive Committee• SOUJava• Goldman Sachs• London JavaCommunity• Alex Terrazas

• JCP starting a program of reform• JSR 348: Towards a new version of the JCP

Tuesday, July 12, 2011

Page 8: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Tuesday, July 12, 2011

Page 9: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Tuesday, July 12, 2011

Page 10: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

\

The rise of multi-core/processor architectures

Tuesday, July 12, 2011

Page 11: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

\

Niagara 1 (2005)8 x 4 = 32

The rise of multi-core/processor architectures

Tuesday, July 12, 2011

Page 12: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

\

Niagara 1 (2005)8 x 4 = 32

Niagara 2 (2007)8 x 8 = 64

The rise of multi-core/processor architectures

Tuesday, July 12, 2011

Page 13: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

\

Niagara 1 (2005)8 x 4 = 32

Niagara 2 (2007)8 x 8 = 64

Rainbow Falls16 x 8 = 128

The rise of multi-core/processor architectures

Tuesday, July 12, 2011

Page 14: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

2002 2004 2006 2008 2010

Not just high end servers

2011

Desktop ... 2

Tuesday, July 12, 2011

Page 15: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Desktop ... notepad ... phone 2 ... 4 ... 8

2002 2004 2006 2008 2010

Not just high end servers

2011

Tuesday, July 12, 2011

Page 16: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

EvolutionRevolution1.0

1.1

1.2

1.31.4

5.0

61996 1997 1998 2000 2002 2004 2006 2010

Tuesday, July 12, 2011

Page 17: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

EvolutionRevolution1.0

1.1

1.2

1.31.4

5.0

61996 1997 1998 2000 2002 2004 2006 2010 2011 2012

78

Tuesday, July 12, 2011

Page 18: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

13

Java SE 7 Release Contents

• Java Language• Project Coin (JSR-334)

• Class Libraries• NIO2 (JSR-203)• Fork-Join framework(JSR-166y)

• Java Virtual Machine• The DaVinci Machine project (JSR-292)• InvokeDynamic bytecode

• Large number of smaller updates

Tuesday, July 12, 2011

Page 19: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

14

<Insert Picture Here>

Section DividerSmall

LanguageChanges

Project Coin

Tuesday, July 12, 2011

Page 20: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

15

coin, n. A piece of small changecoin, v. To create new language

Tuesday, July 12, 2011

Page 21: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

16

Evolving the Language

• Java language principles– Reading is more important than writing– Code should be a joy to read– Code should do what it seems to do

– Simplicity matters– Every “good” feature adds more “bad” weight– Sometimes it is best to leave things out

• One language: with the same meaning everywhere• No dialects

• We will evolve the Java language• But cautiously, with a long term view• “first do no harm”

Tuesday, July 12, 2011

Page 22: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

17

Characteristics of Project Coin

• Productive, intuitive additions• Small language changes

• Small in specification, implementation, testing• No new keywords!• Wary of type system changes

• Coordinate with larger language changes– Project Lambda– Modularity

• One language, one javac

Tuesday, July 12, 2011

Page 23: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

18

Better Integer Literals

• Binary literals

• With underscores for clarity

int mask = 0b101010101010;

int mask = 0b1010_1010_1010;long big = 9_223_783_036_967_937L;

Tuesday, July 12, 2011

Page 24: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

314159265311385876331564300000029979245853500000000

Recognize any of these numbers ?

Tuesday, July 12, 2011

Page 25: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

Recognize any of these numbers ?

3_14159265311_385_8763_315_643_000_000299_792_45853_500_000_000

Tuesday, July 12, 2011

Page 26: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

Recognize any of these numbers ?

3_14159265311_385_8763_315_643_000_000299_792_45853_500_000_000

π

Tuesday, July 12, 2011

Page 27: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

Recognize any of these numbers ?

3_14159265311_385_8763_315_643_000_000299_792_45853_500_000_000

πest. US population

Tuesday, July 12, 2011

Page 28: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

Recognize any of these numbers ?

3_14159265311_385_8763_315_643_000_000299_792_45853_500_000_000

πest. US population

German GDP 2010

Tuesday, July 12, 2011

Page 29: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

Recognize any of these numbers ?

3_14159265311_385_8763_315_643_000_000299_792_45853_500_000_000

πest. US population

German GDP 2010 c m/s

Tuesday, July 12, 2011

Page 30: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

19

Recognize any of these numbers ?

3_14159265311_385_8763_315_643_000_000299_792_45853_500_000_000

πest. US population

German GDP 2010 c m/sCarlos Slim net worth

Tuesday, July 12, 2011

Page 31: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

20

Strings in Switch statements

• Today case label includes integer constants and enum constants

• Strings are constants too (immutable)

Tuesday, July 12, 2011

Page 32: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

21

int monthNameToDays(String s, int year) { if (s.equals(“April”) || s.equals(“June”) || s.equals(“September”) || s.equals(“November”)) return 30; if (s.equals(“January”) || s.equals(“March”) || s.equals(“May”) || s.equals(“July”) || s.equals(“August”) || s.equals(“December”)) return 31; if (s.equals()) ... else ... }

Tuesday, July 12, 2011

Page 33: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

21

int monthNameToDays(String s, int year) { switch(s) { case “April”: case “June”: case “September”: case “November”: return 30; case “January”: case “March”: case “May”: case “July”: case “August”: case “December”: return 31; case “February”: ... default ... }

Tuesday, July 12, 2011

Page 34: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Diamond Operator

Map<Bar, Foo> map1 = new HashMap<Bar, Foo>(); Map<Bar, List<Foo>> map2 = new HashMap<Bar, List<Foo>>(); Map<Bar, List<List<Foo>>> map3 = new HashMap<Bar, List<List<Foo>>>();

Tuesday, July 12, 2011

Page 35: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Diamond Operator

Map<Bar, Foo> map1 = new HashMap<>(); Map<Bar, List<Foo>> map2 = new HashMap<>(); Map<Bar, List<List<Foo>>> map3 = new HashMap<>();

Tuesday, July 12, 2011

Page 36: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

23

Copying a File

InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(dest);

byte[] buf = new byte[8192];int n;

while (n = in.read(buf)) >= 0) out.write(buf, 0, n);

in.close();out.close();

Tuesday, July 12, 2011

Page 37: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

24

Copying a File

InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(dest);

try { byte[] buf = new byte[8192]; int n; while (n = in.read(buf)) >= 0) out.write(buf, 0, n);} finally { in.close(); out.close();}

Tuesday, July 12, 2011

Page 38: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

25

Copying a File

InputStream in = new FileInputStream(src);try { OutputStream out = new FileOutputStream(dest); try { byte[] buf = new byte[8192]; int n; while (n = in.read(buf)) >= 0) out.write(buf, 0, n); } finally { out.close(); }} finally { in.close();}

Tuesday, July 12, 2011

Page 39: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

26

InputStream in = new FileInputStream(src);try { OutputStream out = new FileOutputStream(dest); try { byte[] buf = new byte[8192]; int n; while (n = in.read(buf)) >= 0) out.write(buf, 0, n); } finally { out.close(); }} finally { in.close();}

Exception thrown frompotentially three places.

Details of first two could be lost

Copying a File

Tuesday, July 12, 2011

Page 40: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

27

try-with-resources syntax

try (InputStream in = new FileInputStream(src), OutputStream out = new FileOutputStream(dest)){ byte[] buf = new byte[8192]; int n; while (n = in.read(buf)) >= 0) out.write(buf, 0, n);}

Tuesday, July 12, 2011

Page 41: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

28

try-with-resources syntax

try ResourceSpecification BlockCatchesoptFinallyopt

Tuesday, July 12, 2011

Page 42: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

29

try-with-resources syntax

• Compiler desugars try-with-resources into nested try-finally blocks with variables to track exception state

• ‘Lost’ exceptions are recorded for posterity using a new facility of Throwable - addSuppressedException

• API support in JDK 7• anything with a void close() method is a candidate for use with

this syntax• i.e. anything implementing java.io.Closeable

or its new superinterface java.lang.AutoCloseable• JDBC 4.1 retrofitted as AutoCloseable too

Tuesday, July 12, 2011

Page 43: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

30

More Informative Backtraces

java.io.IOException at Suppress.write(Suppress.java:19) at Suppress.main(Suppress.java:8) Suppressed: java.io.IOException at Suppress.close(Suppress.java:24) at Suppress.main(Suppress.java:9) Suppressed: java.io.IOException at Suppress.close(Suppress.java:24) at Suppress.main(Suppress.java:9)

Tuesday, July 12, 2011

Page 44: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

31

try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc.} catch (ClassNotFoundException cnfe) { log(cnfe) throw cnfe;} catch (InstantiationException ie) { log(ie); throw ie;} catch (NoSuchMethodException nsme) { log(nsme); throw nsme;} catch {InvocationTargetException ite) { log(ite); throw ite;}

Multi-catch with More Precise Rethrow

Tuesday, July 12, 2011

Page 45: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

32

try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc.} catch (Exception e) { log(e) throw e;}

Tempting...

Tuesday, July 12, 2011

Page 46: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

33

try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc.} catch (Exception e) { log(e) throw e;}

Exception by-catch

Catches both checked and unchecked exceptions

Tuesday, July 12, 2011

Page 47: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

34

try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc.} catch (RuntimeException re) { ...} catch (Exception e) { log(e) throw e;}

Exception by-catch

Better

Tuesday, July 12, 2011

Page 48: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

35

try { // Reflective operations calling Class.formName, // Class.newOnstance, Class.getMethod, Method.invoke, etc.} catch (final ClassNotFoundException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { log(e) throw e;}

Multi-catch - best

Tuesday, July 12, 2011

Page 49: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

36Tuesday, July 12, 2011

Page 50: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

37

New I/O 2 (NIO2) Libraries

• Original Java I/O APIs presented challenges for developers • Not designed to be extensible• Many methods do not throw exceptions as expected• rename() method works inconsistently• Developers want greater access to file metadata

• Java NIO2 solves these problems

JSR 203

Tuesday, July 12, 2011

Page 51: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

38

Java NIO2 API Features

• Path is a replacement for File• Biggest impact on developers

• Better directory support• list() method can stream via iterator• Entries can be filtered using regular expressions in API

• Symbolic link support• java.nio.file.Filesystem

• interface to a filesystem (FAT, ZFS, Zip archive, network, etc)• java.nio.file.attribute package

• Access to file metadata

Tuesday, July 12, 2011

Page 52: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

39

The Path Class

• Equivalent of java.io.File in the new API– Immutable

• Have methods to access and manipulate Path • Few ways to create a Path– From Paths and FileSystem

// Make a reference to the pathPath home = Paths.get(“/home/fred”);

// Resolve tmp from /home/fred -> /home/fred/tmpPath tmpPath = home.resolve(“tmp”);

// Create a relative path from tmp -> ..Path relativePath = tmpPath.relativize(home)

File file = relativePath.toFile();

Tuesday, July 12, 2011

Page 53: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

40

File Operations using the Files class

• File copy is really easy– With fine grain control

• File move is supported– Optional atomic move supported

Path src = Paths.get(“/home/fred/readme.txt”);Path dst = Paths.get(“/home/fred/copy_readme.txt”);

Files.copy(src, dst, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);

Path src = Paths.get(“/home/fred/readme.txt”);Path dst = Paths.get(“/home/fred/readme.1st”);

Files.move(src, dst, StandardCopyOption.ATOMIC_MOVE);

Tuesday, July 12, 2011

Page 54: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

41

Directories

• DirectoryStream iterate over entries– Scales to large directories– Uses less resources– Smooth out response time for remote file systems– Implements Iterable and Closeable for productivity

• Filtering support– Build-in support for glob, regex and custom filters

Path srcPath = Paths.get(“/home/fred/src”);

try (DirectoryStream dir = Files.newDirectoryStream(srcPath, “*.{c,h,cpp,hpp,java}”)) {

for (Path file: dir) System.out.println(file.getName());}

Tuesday, July 12, 2011

Page 55: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Using NIO.2 APIs to search a directory

import java.nio.file.*;

Tuesday, July 12, 2011

Page 56: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Using NIO.2 APIs to search a directory

import java.nio.file.*;

Path searchDir = Paths.get("c:/Users");final Path findFile = Paths.get("baby.jpg");

Tuesday, July 12, 2011

Page 57: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Using NIO.2 APIs to search a directory

import java.nio.file.*;

Path searchDir = Paths.get("c:/Users");final Path findFile = Paths.get("baby.jpg");

FileVisitor visitor = new SimpleFileVisitor() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if (file.getName().startsWith(findFile.getName())) { System.out.format("%s%n", findFile); } return FileVisitResult.CONTINUE; }};

Tuesday, July 12, 2011

Page 58: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Using NIO.2 APIs to search a directory

import java.nio.file.*;

Path searchDir = Paths.get("c:/Users");final Path findFile = Paths.get("baby.jpg");

FileVisitor visitor = new SimpleFileVisitor() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if (file.getName().startsWith(findFile.getName())) { System.out.format("%s%n", findFile); } return FileVisitResult.CONTINUE; }};

EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);

Tuesday, July 12, 2011

Page 59: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Using NIO.2 APIs to search a directory

import java.nio.file.*;

Path searchDir = Paths.get("c:/Users");final Path findFile = Paths.get("baby.jpg");

FileVisitor visitor = new SimpleFileVisitor() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if (file.getName().startsWith(findFile.getName())) { System.out.format("%s%n", findFile); } return FileVisitResult.CONTINUE; }};

EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);

Files.walkFileTree(searchDir, opts, Integer.MAX_VALUE, visitor);

Tuesday, July 12, 2011

Page 60: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Using NIO.2 APIs to search a directory

import java.nio.file.*;

Path searchDir = Paths.get("c:/Users");final Path findFile = Paths.get("baby.jpg");

FileVisitor visitor = new SimpleFileVisitor() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if (file.getName().startsWith(findFile.getName())) { System.out.format("%s%n", findFile); } return FileVisitResult.CONTINUE; }};

EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);

Files.walkFileTree(searchDir, opts, Integer.MAX_VALUE, visitor);

Tuesday, July 12, 2011

Page 61: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

43

Concurrency APIs

• JSR166y• Update to JSR166x which was an update to JSR166

• Adds a lightweight task framework• Also referred to as Fork/Join

Tuesday, July 12, 2011

Page 62: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

44

Fork Join Framework• Goal is to take advantage of multiple processor• Designed for task that can be broken down into

smaller pieces– Eg. Fibonacci number fib(10) = fib(9) + fib(8)

if the task is manageably small perform the taskelse fork task into x number of smaller/similar task join the results

Tuesday, July 12, 2011

Page 65: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Concept of Fork/Join Framework

Large, compute-intensive

task

Fork intosubtasks

Image: Gareth Jones

Tuesday, July 12, 2011

Page 66: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Concept of Fork/Join Framework

Large, compute-intensive

task

Fork intosubtasks

Process tasksin parallel

Image: Gareth Jones

Tuesday, July 12, 2011

Page 67: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Concept of Fork/Join Framework

Large, compute-intensive

task

Fork intosubtasks

Process tasksin parallel

Join subtasks as they complete

Image: Gareth Jones

Tuesday, July 12, 2011

Page 68: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

Concept of Fork/Join Framework

Large, compute-intensive

task

Fork intosubtasks

Process tasksin parallel

Join subtasks as they complete

Completedtask

Image: Gareth Jones

Tuesday, July 12, 2011

Page 69: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

46

Key Classes

• ForkJoinPool– Executor service for running ForkJoinTask

• ForkJoinTask– The base class for forkjoin task

• RecursiveAction– A subclass of ForkJoinTask– A recursive resultless task– Implements compute() abstract method to perform

calculation• RecursiveTask– Similar to RecursiveAction but returns a result

Tuesday, July 12, 2011

Page 70: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

©2011 Oracle Corporation

Example: Blurring an image

public class ForkBlur { private int[] mSource; private int mStart; private int mLength; private int[] mDestination; private int mBlurWidth = 15; // Processing window size, should be odd. public ForkBlur(int[] src, int start, int length, int[] dst) { mSource = src; mStart = start; mLength = length; mDestination = dst; }

...

Tuesday, July 12, 2011

Page 71: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

©2011 Oracle Corporation

public class ForkBlur {

... // this is the heavy lifting protected void computeDirectly() { int sidePixels = (mBlurWidth - 1) / 2; for (int index = mStart; index < mStart + mLength; index++) { // Calculate average. float rt = 0, gt = 0, bt = 0; for (int mi = -sidePixels; mi <= sidePixels; mi++) { int mindex = Math.min(Math.max(mi + index, 0), mSource.length - 1); int pixel = mSource[mindex]; rt += (float)((pixel & 0x00ff0000) >> 16) / mBlurWidth; gt += (float)((pixel & 0x0000ff00) >> 8) / mBlurWidth; bt += (float)((pixel & 0x000000ff) >> 0) / mBlurWidth; } // Re-assemble destination pixel. int dpixel = (0xff000000 ) | (((int)rt) << 16) | (((int)gt) << 8) | (((int)bt) << 0); mDestination[index] = dpixel; }

...

}

Example: Blurring an image

Tuesday, July 12, 2011

Page 72: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

©2011 Oracle Corporation

Heavy lifting in parallel using Fork/Join

public class ForkBlur extends RecursiveAction {

protected void compute() { // use the Fork/Join pattern here

} ...

protected void computeDirectly() { // this is still the heavy lifting }

...

}

Tuesday, July 12, 2011

Page 73: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

©2011 Oracle Corporation

Basic concept of Fork/Join

if the task is manageably small perform the taskelse fork task into x number of smaller/similar task join the results

Tuesday, July 12, 2011

Page 74: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

©2011 Oracle Corporation

Application of Fork/Joinprotected static int sThreshold = 100000;

protected void compute() {

// is my portion of the work is small enough ? if (mLength < sThreshold) {

// just do it computeDirectly(); return; } else {

// split my work into two pieces int split = mLength / 2; invokeAll(

new ForkBlur(mSource, mStart, split, mDestination), new ForkBlur(mSource, mStart + split,

mLength - split, mDestination) );

}}

Tuesday, July 12, 2011

Page 75: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

©2011 Oracle Corporation

Running the code

// source image pixels are in src// destination image pixels are in dstForkBlur fb = new ForkBlur(src, 0, src.length, dst);

ForkJoinPool pool = new ForkJoinPool();

// now the work can be executed in parallelpool.invoke(fb);

Tuesday, July 12, 2011

Page 76: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

53

Client Libraries

• Nimbus Look and Feel• Platform APIs for shaped and translucent windows• JLayer (formerly from Swing labs)• Optimised 2D rendering

Tuesday, July 12, 2011

Page 77: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

54

Nimbus Look and Feel

• Better than Metal for cross platform look-and-feel• Introduced in Java SE 6u10, now part of Swing• Not the default L&F

Tuesday, July 12, 2011

Page 78: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

55

JLayer componentEasy enrichment for Swing components

Tuesday, July 12, 2011

Page 79: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

56

JLayer componentThe universal decorator

• Transparent decorator for a Swing component• Controls the painting of its subcomponents• Catches all input and focus events for the whole hierarchy

// wrap your component with JLayerJLayer<JPanel> layer = new JLayer<JPanel>(myPanel);

// custom ui provides all extra functionalitylayer.setUI(myCustomLayerUI);

// add the layer as usual componentframe.add(layer);

Tuesday, July 12, 2011

Page 80: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

57

The DaVinci Machine Project (JSR-292)(A multi-language renaissance for the JVM)

Better

Tuesday, July 12, 2011

Page 81: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

58

Languages Running on the JVM

51

Clojure

Tcl

JavaScript

v-language

CAL

Sather

Funnel

MiniPLAN

Lisp

Scheme

Basic

Logo JHCR

TermWare

Drools

Prolog

LLP

JESS

Eiffel

Smalltalk

C#

G

GroovyNice

Anvil

Hojo

Correlate

Ada

Bex Script

Tea

PHP

PhobosSleep

FScript

JudoScript

JRuby

ObjectScript

Jickle

Yoix

Simkin

BeanShell

DawnWebL

iScript

Jython

Pnuts

Yassl

Forth

PiccolaSALSA

Processing

Zigzag

Tiger

Tiger

Icon Pascal

Oberon

Modula-2

Luck

E

Rexx JavaFX Script Scala

Tuesday, July 12, 2011

Page 82: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

59

Languages Like Virtual Machines

• Programming languages need runtime support• Memory management / Garbage collection• Concurrency control• Security• Reflection• Debugging integration• Standard libraries

• Compiler writers have to build these from scratch• Targeting a VM allows reuse of infrastructure

• DaVinci makes languages like the JVM more !

Tuesday, July 12, 2011

Page 83: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

60

JVM Specification

“The Java virtual machine knows nothing about the Java

programming language, only of a particular binary format, the class

file format.” 1.2 The Java Virtual Machine Spec.

Tuesday, July 12, 2011

Page 84: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

61

CallSite and MethodHandle

• invokedynamic linked to a CallSite– CallSite can be linked or unlinked– CallSite holder of MethodHandle

• MethodHandle is a directly executable reference to an underlying method, constructor, field– Can transform arguments and return type– Transformation – conversion, insertion, deletion, substitution

Tuesday, July 12, 2011

Page 85: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

62

Miscellaneous Things

• Security• Eliptic curve cryptography• TLS 1.2

• JAXP 1.4.4• JAX-WS 2.2• JAXB 2.2• ClassLoader architecture changes• close() for URLClassLoader• Javadoc support for CSS

Tuesday, July 12, 2011

Page 86: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

63

JDK 7 Platform Support

• Windows x86• Server 2008, Server 2008 R2, 7 & 8 (when it GAs)• Windows Vista, XP

• Linux x86• Oracle Linux 5.5+, 6.x• Red Hat Enterprise Linux 5.5+, 6.x• SuSE Linux Enterprise Server 10.x, 11.x• Ubuntu Linux 10.04 LTS, 11.04

• Solaris x86/SPARC• Solaris 10.9+, 11.x

• Apple OSX x86• will be supported post-GA, detailed plan TBD

Note: JDK 7 should run on pretty much any Windows/Linux/Solaris. These configurations are the ones primarily tested by Oracle, and for which we provide commercial support.

Tuesday, July 12, 2011

Page 87: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

JVM Convergence – Forward lookingProject “HotRockit”

• Hotspot 21• Java SE 7 Support• Rebranding• Improved JMX

Agent• Command line

servicability tool (jrcmd)

--- Premium ---• Improved JRockit

Mission Control Console support

JDK 7 GA – 07/11

• Hotspot 22• Performance• Enable large heaps

with reasonable latencies

JDK 7u2

• Hotspot 23• More performance• Improved command

line servicability (jcmd)

• Enable large heaps with consistent reasonable latencies

• No PermGen

--- Premium ---• Complete JRockit

Flight Recorder Support

JDK 7uX

• Hotspot24• Java SE 8 Support• All performance

features from JRockit ported

• All servicability features from JRockit ported• Compiler controls• Verbose logging --- Premium ---

• JRockit Mission Control Memleak Tool Support

• Soft Real Time GC

JDK 8 GA

Tuesday, July 12, 2011

Page 88: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

65

More Project CoinSmall Language Changes

Project Lambda (JSR 335)Closures and lambda expressionsBetter support for multi-core processors

Project Jigsaw (JSR-294)Modularising the Java Platform

Java SE 8

Tuesday, July 12, 2011

Page 89: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

66

Conclusions

• Java SE 7• Incremental changes• Evolutionary, not revolutionary• Good solid set of features to make developers life easier

• Java SE 8• Major new features: Modularisation and Closures• More smaller features to be defined

• Java continues to grow and adapt to the changing world of IT

Tuesday, July 12, 2011

Page 90: Tuesday, July 12, 2011 - .NET, Java, and Mobile …...Changes Project Coin Tuesday, July 12, 2011 15 coin, n. A piece of small change coin, v. To create new language Tuesday, July

67Tuesday, July 12, 2011