<Insert Picture Here>
Agenda
• Project Coin• Modularity• Closures
<Insert Picture Here>
Section DividerSmallSmall
(Language)(Language)ChangesChanges
Project CoinProject Coin
Better Integer Literal• Binary literals
• With underscores for clarity
int mask = 0b101010101010;
int mask = 0b1010_1010_1010;long big = 9_223_783_036_967_937L;
String Switch Statement• Today case label includes integer constants and enum
constants• Strings are constants too
Discriminating Strings Today
int monthNameToDays(String s, int year) {
if("April".equals(s) || "June".equals(s) ||"September".equals(s) ||"November".equals(s))
return 30;
if("January".equals(s) || "March".equals(s) ||"May".equals(s) || "July".equals(s) ||"August".equals(s) || "December".equals(s))
return 31;
if("February".equals(s))...
With String Switchint 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:...
Inferring Types with <>• Pre generics
• With generics
• With diamond (<>) to infer the type
List strList = new ArrayList();
List<String> strList = new ArrayList<String>();
List<Map<String, List<String>> strList =new ArrayList<Map<String, List<String>>();
List<String> strList = new ArrayList<>();
List<Map<String, List<String>> strList =new ArrayList<>();
Exceptions Galoretry {
...} 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
try {// Reflective operations calling Class.forName,// Class.newInstance, Class.getMethod,// Method.invoke, etc.
} catch(final ClassNotFoundException |InstantiationException |NoSuchMethodException |InvocationTargetException e) {
log(e);throw e;
}
Copying Streams
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);
• How to make this piece of code robust?
InputStream in = null;OutputStream out = null;try {
in = new FileInputStream(src);} catch (FileNotFoundException fne) {
return;}try {
out = new FileOutputStream(dst);try {
byte[] buf = new byte[8192];int n;while ((n = in.read(buf)) >= 0)
out.write(buf, 0, n);} catch (IOException ioe) {
//Do something} finally {
try {out.flush();out.close();
} catch (IOException ioe) { }}
} catch (FileNoutFoundException fne) {} finally {
try {in.close();
} catch (IOException ioe) { }}
Automatic Resource Management
• API support– New interface java.lang.AutoClosable– java.io.Closable– JDBC 4.1 retrofitted to use AutoClosable
try (InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(ds)) {byte[] buf = new byte[8192];int n;
while ((n = in.read(buf)) >= 0)out.write(buf, 0, n);
}
ModularityModularityProject JigsawProject Jigsaw
Modularity – the Solution• “JAR hell”
– Eliminates class path– Package modules for automatic download & install– Generate native packages – deb, rpm, ips, etc
• Performance – download time, startup time– Incremental download → fast classloading– Remembering startup classes
• Platform scalability – down to small devices– SE subset can fit into small devices
Modularity Goals• Grouping• Dependencies• Versioning• Encapsulation• Optional modules• Virtual modules
Packages
package com.foo.app
public class Main {
public class Other {
package com.foo.app.ui
public class Shell {
com.foo.app.Main
com.foo.app.Other
com.foo.app.ui.Shell
com.foo.app
module-info.java
module com.foo.app @ 1.0.0 {class com.foo.app.Mainprovides com.foo.app.lib @ 1.0.0;
}
Entry point
Module name Version
Virtual module
com.foo.app.Main
com.foo.app.Other
com.foo.app.ui.Shell
com.foo.app
module-info.java
com.foo.app
org.bar.lib
com.foo.lib
edu.baz.util
module com.foo.app @ 1.0.0 {class com.foo.app.Mainrequires com.foo.lib @ 2.1-alpha;provides com.foo.app.lib @ 1.0.0;
}
Dependency
import com.foo.lib.*;public class Main {
module-info.java
com.foo.app
org.bar.lib
com.foo.lib
edu.baz.util
com.foo.extra
Optional modules
module com.foo @ 1.0.0 {class com.foo.app.Mainrequires com.foo.lib @ 2.1-alpha;provides com.foo.app.lib @ 1.0.0;requires optional com.foo.extra;
}
module-info.java
com.foo.app
com.foo.secret
org.bar.lib
com.foo.lib
edu.baz.util
com.foo.extramodule com.foo.secret @ 1 {permits com.foo.lib;
}
Encapsulating modules
Packaging
$ javac -modulepath mods src/com.foo.app/...
$ ls mods com.foo.app/com.foo.extra/com.foo.lib/
Packaging
$ javac -modulepath mods src/com.foo.app/...
$ ls mods com.foo.app/com.foo.extra/com.foo.lib/
$ ls -R mods/com.foo.appmods/com.foo.app/com/foo/app/Main.classmods/com.foo.app/com/foo/app/Other.classmods/com.foo.app/com/foo/ui/Shell.class
Packaging
$ javac -modulepath mods src/com.foo.app/...
$ ls mods com.foo.app/com.foo.extra/com.foo.lib/
$ jpkg -modulepath mods jmod \com.foo.app com.foo.extra com.foo.lib
Library
org.bar.lib
edu.baz.util
./mlib$ ls *[email protected]@[email protected]
$ jmod -L mlib install \ $EXT/edu.baz.util@*.jmod \ $EXT/org.bar.lib@*.jmod
Library
$ ls *[email protected]@[email protected]
$ jmod -L mlib install \ *.jmod
$ jmod -L mlib lscom.foo.app @ 1.0.0com.foo.extra @ 0.9acom.foo.lib @ 1.0.2edu.baz.util @ 5.2_11org.bar.lib @ 2.1-alpha
com.foo.app
org.bar.lib
com.foo.lib
edu.baz.util
com.foo.extra
./mlib
Execution
$ java -L mlib \ -m com.foo.app
Welcome to Foo...
com.foo.app
org.bar.lib
com.foo.lib
edu.baz.util
com.foo.extra
./mlib
Library Delegation
ORACLE CONFIDENTIAL
com.foo.app
org.bar.lib
com.foo.lib
edu.baz.util
com.foo.extra
jdk.base
jdk.boot
jdk.desktop
jdk.xml
$JRE/lib/modules
./mlib
Repositories
ORACLE CONFIDENTIAL
jdk.base
jdk.boot
jdk.desktop
jdk.xml
$JRE/lib/modules
http://jig.sfbay/linux/x86
...
$ jmod add-repo http://jig.sfbay
$ jmod install jdk.toolsDownloading jdk.tools@7-ea ...Configuring jdk.tools@7-ea ...
Native Packages
Native Packages
$ jpkg -m mods deb com.foo.app com.foo.lib
com.foo.app-1.0.deb
com.foo.lib-3.22.deb
Requires: com.foo.lib >= 3.0
<Insert Picture Here>
Section Divider
ClosureClosureProject LambdaProject Lambda
Motivation for Lambda Project• Moore's law, multi core chip• Library is one of Java's key strength
– Better libraries are key to making parallelization better
• Higher level operations tend to improve readability of code as well as performance
• Without more language support for parallel idioms, people will instinctively reach for serial idioms
For loop – External Iteration
• Code is inherently serial– Stateful – use of > and highestScore– Client (for loop) of students determines iteration mechanism
• Serial – from first to last – no requirement for ordering
Collection<Student> students = ...
double highestScore = 0.0;for (Student s : students) {
if ((s.gradYear == 2010) && (s.score > highestScore))
highestScore = s.score;}
Hypothetical Collection Methods
• Assume collection has filter, map, reduce, max, etc.
• Not inherently serial– students traversal not determined by developer
• Anonymous inner class!– Too much boiler plate code
double highestScore = students.filter(new Predicate<Student>() {
public boolean isTrue(Student s) {return s.gradYear == 2010;
}}).map(new Extractor<Student,Double>() {
public Double extract(Student s) {return s.score;
}}).max();
Introducing Lambda Expressions
• Lambda expressions introduced with #– Signal to the JVM to defer execution of the code
• Zero or more formal parameters– Like methods
• Body may be an expression or statement– If expression, no need 'return' or ';'– Unlike methods
double highestScore = students.filter(#{ Student s -> s.gradYear == 2010 }).map(#{ Student s -> s.score }).max();
Lambda Expression
#{ Student s -> s.gradYear == 2010 }
Denotes lambda expression
Body
Formal parameter(s)
Lambda Expressions – Internal Iteration
• Cleaner syntax• Potentially faster because collection (implementation)
determines how to iterate– Opportunities for lazy evaluation– Opportunities for parallelism
double highestScore = students.filter(#{ Student s -> s.gradYear == 2010 }).map(#{ Student s -> s.score }).max();
Lambda Expression Type
• A function type from Student to boolean• Java does not have function types
– But have single method interfaces (or abstract classes)
– “Function type” from T to int
#{ Student s → s.gradYear == 2010 }
public interface Comparable<T> {public int compareTo(T object);
}
SAM Types• SAM type is an interface or abstract class with a
Single Abstract Method– Examples of existing SAM type in JDK
• No special syntax to declare SAM type
interface Runnable { void run(); }interface Callable<T> { T call(); }interface ActionListener {
void actionPerformed(ActionEvent evt); }interface Comparable<T> {
int compareTo(T obj); }
SAM Conversion• Infers a SAM type from a lambda expression
– Same parameter types and arity as SAM type's method– Return type compatible with SAM type's method– Checked exceptions compatible with SAM type's method
• SAM type's method name is not relevant– Almost like duck typing
ActionListener listener = #{ ActionEvent aEvt ->System.out.println(“hello world”); });
//void run();Runnable run = #{ ->
System.out.println(“hello world”); });//void dispatch();ActiveEvent event = #{ ->
System.out.println(“hello world”); });
Benefits of SAM Type• Target typing – inferring types from SAM type's
method– Can omit formal parameter type
• Compatibility with existing libraries
• No special method to invoke lambda expression– Just call the SAM type's method
button.addActionListener(#{ aEvt ->System.out.println(“hello”); });
ActionListener listener = #{ ... };listener.actionPerformed(new ActionEvent( ... ));
button.addActionListener(#{ aEvt ->System.out.println(“hello”); });
We encourage you to use the newly minted corporate tagline“Software. Hardware. Complete.” at the end of all your presentations.This message should replace any reference to our previous corporate tagline “Oracle Is the Information Company.”