KEMBAR78
Java 7, 8 & 9 - Moving the language forward | PPTX
7, 8 & 9
 Moving the language forward
by Mario Fusco
Red Hat – Senior Software Engineer
mario.fusco@gmail.com
twitter: @mariofusco
New in Java 7 – Released in July 2011
• JSR 292: Support for dynamically-typed
  languages (InvokeDynamic)
• JSR 334: Small language enhancements
  (Project Coin)
• JSR 166y: Concurrency and collection updates
  (Fork/Join framework)
• JSR 203: More new I/O APIs for the Java
  platform (NIO.2)
Switch on Strings
switch(dayOfWeek) {
    case "Monday" :
        System.out.println("Start of week");
        break;
    ....
    case "Sunday" :
        System.out.println("Hurrey.. its weekend");
        break;
}
Improved Exception handling
public void printFileContent(String fileName)
                                 throws IOException {
    Configuration cfg = null;
    try {
        String fileText = getFile(fileName);
        //...code to print content
    } catch (FileNotFoundException |
                       FileLockInterruptionException e1) {
        System.err.println("error while opening file");
        throw e1;
    } catch (ParseException e2) {
        System.err.println("Error processing file");
    }
}
Try with resources
try ( FileOutputStream fos = new FileOutputStream(file);
      InputStream is = url.openStream() )
{
    byte[] buf = new byte[4096];
    int len;
    while ((len = is.read(buf)) > 0) {
        fos.write(buf, 0, len);
    }
}
Diamond Operator
Map<Person, List<Address>>



  Underscores in numeric literals

static final int ONE_MILLION = 1_000_000
Fork/Join Framework
public class FileSizeFinder extends RecursiveTask<Long> {
    private final File file;
    public FileSizeFinder(File theFile) { file = theFile; }

    @Override public Long compute() {
        long size = 0;
        if (file.isFile()) return file.length();
        File[] children = file.listFiles();
        if (children == null) return size;
        List<ForkJoinTask<Long>> tasks = new ArrayList<>();
        for (File child : children) {
           if (child.isFile()) size += child.length();
           else tasks.add(new FileSizeFinder(child));
        }
        for (ForkJoinTask<Long> task : invokeAll(tasks)) size += task.join();
        return size;
    }
}

long total = new ForkJoinPool().invoke(new FileSizeFinder(new File(rootName)));
Coming in Java 8 – Early (hopefully) 2013
• JSR 335: Project Lambda
• JSR TBD: Language support for collections
• JSR 308: Annotations on Java types
• JSR 310: Date and Time API (from JodaTime)
• JSR 294: Language and VM support for
  modular programming
• JSR TBD: Project Jigsaw (Modularization)
Project Lambda - Background
public interface Comparator<T> {              Functional
    int compare(T o1, T o2);
}                                             Interface
Collections.sort(strings, new Comparator<String>() {
    public int compare(String s1, String s2) {
        return s1.compareToIgnoreCase(s2);
    }
});


•   Bulky syntax
•   Confusion surrounding the meaning of names and this
•   Inability to capture non-final local variables
•   Inability to abstract over control flow
Lambda Expressions
Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));



                     Target typing
Comparator<String> c = (s1, s2) -> s1.compareToIgnoreCase(s2);



                 Method reference
Collections.sort(strings, String::compareToIgnoreCase);
Lambda support for collections
double maxScore = 0;
for (Student s : students) {
    if (s.gradYear == 2011) {
        maxScore = Math.max(maxScore, s.score);
    }
}



double maxScore = students.parallel()
                    .filter(s -> s.gradYear == 2011)
                    .map(s -> s.score)
                    .reduce(0, Math::max);
Default methods
interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove();

    void skip(int i) default {
        for (; i > 0 && hasNext(); i--) next();
    }

    boolean filter default
        Iterables::filter
}
What to expect from Java 9

Java 7, 8 & 9 - Moving the language forward

  • 1.
    7, 8 &9 Moving the language forward by Mario Fusco Red Hat – Senior Software Engineer mario.fusco@gmail.com twitter: @mariofusco
  • 2.
    New in Java7 – Released in July 2011 • JSR 292: Support for dynamically-typed languages (InvokeDynamic) • JSR 334: Small language enhancements (Project Coin) • JSR 166y: Concurrency and collection updates (Fork/Join framework) • JSR 203: More new I/O APIs for the Java platform (NIO.2)
  • 3.
    Switch on Strings switch(dayOfWeek){ case "Monday" : System.out.println("Start of week"); break; .... case "Sunday" : System.out.println("Hurrey.. its weekend"); break; }
  • 4.
    Improved Exception handling publicvoid printFileContent(String fileName) throws IOException { Configuration cfg = null; try { String fileText = getFile(fileName); //...code to print content } catch (FileNotFoundException | FileLockInterruptionException e1) { System.err.println("error while opening file"); throw e1; } catch (ParseException e2) { System.err.println("Error processing file"); } }
  • 5.
    Try with resources try( FileOutputStream fos = new FileOutputStream(file); InputStream is = url.openStream() ) { byte[] buf = new byte[4096]; int len; while ((len = is.read(buf)) > 0) { fos.write(buf, 0, len); } }
  • 6.
    Diamond Operator Map<Person, List<Address>> Underscores in numeric literals static final int ONE_MILLION = 1_000_000
  • 7.
    Fork/Join Framework public classFileSizeFinder extends RecursiveTask<Long> { private final File file; public FileSizeFinder(File theFile) { file = theFile; } @Override public Long compute() { long size = 0; if (file.isFile()) return file.length(); File[] children = file.listFiles(); if (children == null) return size; List<ForkJoinTask<Long>> tasks = new ArrayList<>(); for (File child : children) { if (child.isFile()) size += child.length(); else tasks.add(new FileSizeFinder(child)); } for (ForkJoinTask<Long> task : invokeAll(tasks)) size += task.join(); return size; } } long total = new ForkJoinPool().invoke(new FileSizeFinder(new File(rootName)));
  • 8.
    Coming in Java8 – Early (hopefully) 2013 • JSR 335: Project Lambda • JSR TBD: Language support for collections • JSR 308: Annotations on Java types • JSR 310: Date and Time API (from JodaTime) • JSR 294: Language and VM support for modular programming • JSR TBD: Project Jigsaw (Modularization)
  • 9.
    Project Lambda -Background public interface Comparator<T> { Functional int compare(T o1, T o2); } Interface Collections.sort(strings, new Comparator<String>() { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); • Bulky syntax • Confusion surrounding the meaning of names and this • Inability to capture non-final local variables • Inability to abstract over control flow
  • 10.
    Lambda Expressions Collections.sort(strings, (s1,s2) -> s1.compareToIgnoreCase(s2)); Target typing Comparator<String> c = (s1, s2) -> s1.compareToIgnoreCase(s2); Method reference Collections.sort(strings, String::compareToIgnoreCase);
  • 11.
    Lambda support forcollections double maxScore = 0; for (Student s : students) { if (s.gradYear == 2011) { maxScore = Math.max(maxScore, s.score); } } double maxScore = students.parallel() .filter(s -> s.gradYear == 2011) .map(s -> s.score) .reduce(0, Math::max);
  • 12.
    Default methods interface Iterator<E>{ boolean hasNext(); E next(); void remove(); void skip(int i) default { for (; i > 0 && hasNext(); i--) next(); } boolean filter default Iterables::filter }
  • 13.
    What to expectfrom Java 9