![Page 2: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/2.jpg)
<Insert Picture Here>
Agenda
• Project Coin• Modularity• Closures
![Page 3: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/3.jpg)
<Insert Picture Here>
Section DividerSmallSmall
(Language)(Language)ChangesChanges
Project CoinProject Coin
![Page 4: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/4.jpg)
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;
![Page 5: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/5.jpg)
String Switch Statement• Today case label includes integer constants and enum
constants• Strings are constants too
![Page 6: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/6.jpg)
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))...
![Page 7: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/7.jpg)
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:...
![Page 8: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/8.jpg)
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<>();
![Page 9: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/9.jpg)
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;
}
![Page 10: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/10.jpg)
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;
}
![Page 11: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/11.jpg)
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?
![Page 12: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/12.jpg)
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) { }}
![Page 13: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/13.jpg)
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);
}
![Page 14: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/14.jpg)
ModularityModularityProject JigsawProject Jigsaw
![Page 15: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/15.jpg)
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
![Page 16: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/16.jpg)
Modularity Goals• Grouping• Dependencies• Versioning• Encapsulation• Optional modules• Virtual modules
![Page 17: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/17.jpg)
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
![Page 18: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/18.jpg)
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
![Page 19: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/19.jpg)
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 {
![Page 20: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/20.jpg)
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;
}
![Page 21: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/21.jpg)
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
![Page 22: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/22.jpg)
Packaging
$ javac -modulepath mods src/com.foo.app/...
$ ls mods com.foo.app/com.foo.extra/com.foo.lib/
![Page 23: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/23.jpg)
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
![Page 24: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/24.jpg)
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
![Page 26: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/26.jpg)
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
![Page 27: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/27.jpg)
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
![Page 28: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/28.jpg)
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
![Page 29: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/29.jpg)
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
![Page 30: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/30.jpg)
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 ...
![Page 31: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/31.jpg)
Native Packages
![Page 32: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/32.jpg)
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
![Page 33: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/33.jpg)
<Insert Picture Here>
Section Divider
ClosureClosureProject LambdaProject Lambda
![Page 34: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/34.jpg)
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
![Page 35: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/35.jpg)
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;}
![Page 36: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/36.jpg)
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();
![Page 37: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/37.jpg)
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();
![Page 38: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/38.jpg)
Lambda Expression
#{ Student s -> s.gradYear == 2010 }
Denotes lambda expression
Body
Formal parameter(s)
![Page 39: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/39.jpg)
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();
![Page 40: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/40.jpg)
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);
}
![Page 41: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/41.jpg)
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); }
![Page 42: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/42.jpg)
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”); });
![Page 43: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/43.jpg)
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”); });
![Page 44: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/44.jpg)
![Page 45: JDK 7 and Beyond - Oracle...Modularity Project Jigsaw Modularity – the Solution • “JAR hell” – Eliminates class path – Package modules for automatic download & install](https://reader035.vdocuments.us/reader035/viewer/2022062920/5f01fa017e708231d401f6f9/html5/thumbnails/45.jpg)
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.”