KEMBAR78
Java 7 workshop | PDF
Java 7
Anything new under the Sun?
Java 7
Hands-on
 Java 8+
Versions
JDK 1.0     Jan, 1996
JDK 1.1     Feb, 1997
J2SE 1.2    Dec, 1998
J2SE 1.3    May, 2000
J2SE 1.4    Feb, 2002
J2SE 5.0    Sep, 2004
Java SE 6   Dec, 2006
Java SE 7   Jul, 2011
Plans
    Plan A


      JDK 7
As currently planned




   Mid 2012
Plans
    Plan A                             Plan B

                               JDK7                 JDK8
      JDK 7                 minus Lambda,
                                                Lambda, Jigsaw,
As currently planned      Jigsaw, and part of
                                                and rest of Coin
                                 Coin



   Mid 2012                  Mid 2011            End 2012
Platform Features
    API Features
Language Features
Dynamic Language
    Support
JVM has no support for dynamic
languages:
• Existing JVM instruction set is
  statically typed
• Limited support for dynamically
  modifying existing classes and
  methods
Dynamic Language
    Support
Java 7 adds:
• a new invokedynamic instruction at the
   JVM level
• ability to change classes and
   methods at runtime dynamically in a
   production environment.
Other Platform
       Features

•   JVM performance enhancements
•   Upgrade class-loader architecture
•   Garbage-First Collector
•   New JavaDoc layout
Platform Features
    API Features
Language Features
Fork/Join
NIO.2

• The FileVisitor API
• Directory watching
• File attributes
• Custom filesystem providers (ZIP)
Other API Features
• Updated XML stacks
• JDBC 4.1
• MethodHandles
• Unicode 6.0 and Locale enhancements
• Elliptic-curve cryptography
• Enhanced MBeans
Platform Features
    API Features
Language Features
Strings in switch
String line = in.readLine();

switch (line) {
  case "hello":
   System.out.println("hello world!");
    return;
  case "cafe":
   System.out.println("cafebabe!");
    return;
  default:
   System.out.println("wrong...");
}
Literals

int binary = 0b1001_0000_1111_0010;
System.out.println(binary);
System.out.println(Integer.toBinaryString(binary));

int readableMillion = 1_000_000;
System.out.println(readableMillion);

int readableBillion = 1_000_000_000;
System.out.println(readableBillion);
Multi-catch
/* Java 6 and older */
} catch (IOException e) {
     logger.log(e);
     throw e;
} catch (SQLException e) {
     logger.log(e);
     throw e;
}

/* Java 7 */
} catch (IOException | SQLException e) {
     logger.log(e);
     throw e;
}
Precise rethrow
public void preciseRethrow() throws IOException {
    try {
        new SimpleDateFormat("yyyyMMdd").parse("foo");
        new FileReader("file.txt").read();
    } catch(ParseException e) {
        System.out.println(e.getMessage());
    } catch(Exception e) {
        System.out.println(e.getMessage());
        throw e;
    }
}
“Diamond”
/* Java 5 & 6 */
Map<String, List<String>> myMap = new HashMap<String,
List<String>>();

/* Java 7 */
Map<String, List<String>> myMap = new HashMap<>();

/* Be warned, the compiler still generates an unchecked
conversion warning! */
Map<String, List<String>> myMap = new HashMap();
Try-with-resources

File file = new File("input.txt");

try (InputStream is = new FileInputStream(file)) {
    // do something with this input stream
} catch (FileNotFoundException ex) {
    System.err.println(file.getAbsolutePath());
}
Java 7
Hands-on
 Java 8+
IMDb

We’re building an application which
reads IMDb data and parses it to
determine which actor has made the
best films.
Read files

• Open project SequentialWithoutIO
• Update root field in class IMDB
• Find method readFilms() and
  readActors() in class IMDB

• Implement!
NIO.2 Hints
  /** Read a UTF-16 file like ratings-clean.txt **/
try (BufferedReader reader = Files.newBufferedReader(
    file, Charset.forName(“UTF-16”)))

/** Read all actor files in a directory **/
try (DirectoryStream<Path> stream =
 Files.newDirectoryStream(dir, “actor*.txt”))

/** Read all lines in file **/
Files.readAllLines(entry, Charset.forName(“UTF-8”));
Parse files
• Open project SequentialParserWithIO
  (or continue with your previous project)
• Update root field in class IMDB
• Find method compute() in classes
  ActorParseTask and FilmParseTask

• Implement!
Fork/Join Hints
/** Start computing with checking if the work is small
enough to do sequentially **/

/** Fork! **/
Collection<ParseTask> completedForks = invokeAll(
    /** give a list work packages **/);

/** Join! **/
for (ParseTask task : completedForks)
/** Join all the results of the tasks and return this
collection **/
Java 7
Hands-on
 Java 8+
Features
• Project Coin 2
 • List literals: [1,   2, 3]

 • Map literals: [“a”:    1, “b”: 2, “c”: 3]

• Project Lambda: (x,      y) => x + y

• Type annotations: List<@NonNull        Object>

• Superpackages
Features
• Project Jigsaw
 • JDK modularity
 • Modular and versioned applications
• Joda Time
• Project HotRockit
• Porting JDeveloper to Netbeans
Java 8 workshop
    December 2012
      Hopefully...

Java 7 workshop

  • 1.
    Java 7 Anything newunder the Sun?
  • 2.
  • 3.
    Versions JDK 1.0 Jan, 1996 JDK 1.1 Feb, 1997 J2SE 1.2 Dec, 1998 J2SE 1.3 May, 2000 J2SE 1.4 Feb, 2002 J2SE 5.0 Sep, 2004 Java SE 6 Dec, 2006 Java SE 7 Jul, 2011
  • 4.
    Plans Plan A JDK 7 As currently planned Mid 2012
  • 5.
    Plans Plan A Plan B JDK7 JDK8 JDK 7 minus Lambda, Lambda, Jigsaw, As currently planned Jigsaw, and part of and rest of Coin Coin Mid 2012 Mid 2011 End 2012
  • 6.
    Platform Features API Features Language Features
  • 7.
    Dynamic Language Support JVM has no support for dynamic languages: • Existing JVM instruction set is statically typed • Limited support for dynamically modifying existing classes and methods
  • 8.
    Dynamic Language Support Java 7 adds: • a new invokedynamic instruction at the JVM level • ability to change classes and methods at runtime dynamically in a production environment.
  • 9.
    Other Platform Features • JVM performance enhancements • Upgrade class-loader architecture • Garbage-First Collector • New JavaDoc layout
  • 10.
    Platform Features API Features Language Features
  • 11.
  • 12.
    NIO.2 • The FileVisitorAPI • Directory watching • File attributes • Custom filesystem providers (ZIP)
  • 13.
    Other API Features •Updated XML stacks • JDBC 4.1 • MethodHandles • Unicode 6.0 and Locale enhancements • Elliptic-curve cryptography • Enhanced MBeans
  • 14.
    Platform Features API Features Language Features
  • 15.
    Strings in switch Stringline = in.readLine(); switch (line) { case "hello":    System.out.println("hello world!");     return;   case "cafe":    System.out.println("cafebabe!");     return;   default:    System.out.println("wrong..."); }
  • 16.
    Literals int binary =0b1001_0000_1111_0010; System.out.println(binary); System.out.println(Integer.toBinaryString(binary)); int readableMillion = 1_000_000; System.out.println(readableMillion); int readableBillion = 1_000_000_000; System.out.println(readableBillion);
  • 17.
    Multi-catch /* Java 6and older */ } catch (IOException e) { logger.log(e); throw e; } catch (SQLException e) { logger.log(e); throw e; } /* Java 7 */ } catch (IOException | SQLException e) { logger.log(e); throw e; }
  • 18.
    Precise rethrow public voidpreciseRethrow() throws IOException {     try {         new SimpleDateFormat("yyyyMMdd").parse("foo");         new FileReader("file.txt").read();     } catch(ParseException e) {         System.out.println(e.getMessage());     } catch(Exception e) {         System.out.println(e.getMessage());         throw e;     } }
  • 19.
    “Diamond” /* Java 5& 6 */ Map<String, List<String>> myMap = new HashMap<String, List<String>>(); /* Java 7 */ Map<String, List<String>> myMap = new HashMap<>(); /* Be warned, the compiler still generates an unchecked conversion warning! */ Map<String, List<String>> myMap = new HashMap();
  • 20.
    Try-with-resources File file =new File("input.txt"); try (InputStream is = new FileInputStream(file)) { // do something with this input stream } catch (FileNotFoundException ex) { System.err.println(file.getAbsolutePath()); }
  • 21.
  • 22.
    IMDb We’re building anapplication which reads IMDb data and parses it to determine which actor has made the best films.
  • 23.
    Read files • Openproject SequentialWithoutIO • Update root field in class IMDB • Find method readFilms() and readActors() in class IMDB • Implement!
  • 24.
    NIO.2 Hints /** Read a UTF-16 file like ratings-clean.txt **/ try (BufferedReader reader = Files.newBufferedReader( file, Charset.forName(“UTF-16”))) /** Read all actor files in a directory **/ try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, “actor*.txt”)) /** Read all lines in file **/ Files.readAllLines(entry, Charset.forName(“UTF-8”));
  • 25.
    Parse files • Openproject SequentialParserWithIO (or continue with your previous project) • Update root field in class IMDB • Find method compute() in classes ActorParseTask and FilmParseTask • Implement!
  • 26.
    Fork/Join Hints /** Startcomputing with checking if the work is small enough to do sequentially **/ /** Fork! **/ Collection<ParseTask> completedForks = invokeAll( /** give a list work packages **/); /** Join! **/ for (ParseTask task : completedForks) /** Join all the results of the tasks and return this collection **/
  • 27.
  • 28.
    Features • Project Coin2 • List literals: [1, 2, 3] • Map literals: [“a”: 1, “b”: 2, “c”: 3] • Project Lambda: (x, y) => x + y • Type annotations: List<@NonNull Object> • Superpackages
  • 29.
    Features • Project Jigsaw • JDK modularity • Modular and versioned applications • Joda Time • Project HotRockit • Porting JDeveloper to Netbeans
  • 30.
    Java 8 workshop December 2012 Hopefully...