Java 8 – Java Finally Gets Awesome-ish



Java 8 – Java Finally Gets Awesome-ish

0 0


TeamJava8Lesson

Presentation from BYU GENESYS team meeting about the new features in Java 8

On Github ThatJoeMoore / TeamJava8Lesson

Java 8

Java Finally Gets Awesome-ish

Grab the slides at http://tiny.cc/OITJava8

Background

1996 - JDK 1.0

NOT the Java we know and love-hate.

2004 - J2SE 5.0

J2SE = Java 2 Standard Edition (Don't Ask)
  • Generics
  • Annotations
  • Enums
  • Varargs
  • foreach loop: for (String each : strings)

2006 - Java 6

  • Minor improvements
  • You can drop the 'public' in interface methods:
    public interface MyInterface {
        void myMethod();
    
        //Same as:
        public void myMethod();
    }
                        

2011 - Java 7

  • Support for dynamic languages (invokedynamic)
  • Strings in switch
  • Try-With-Resources
  • Better Catch Blocks
  • Diamond Operator
  • Binary literals

End-of-life: April 14, 2015

2014 - Java 8

  • Lambda Expressions (Closures)
  • Default Method Implementions
  • Optional Class
  • Streams API
  • Nashorn
  • New Date and Time API

2016? - Java 9

  • Modular JVM - only load what you need
  • Money and Currency API
  • Automatic Parallelization

2018? - Java 10

  • Value Types
  • 64-bit arrays
  • Better native code
  • Better primitives

Let's Review Java 7...

Strings in Switch

Old

if (string.equals("hi") || string.equals("bye")) {
    return "ciao";
} else if (string.equals("hello")) {
    return "salve";
} else {
    return "non capisco " + string;
}
                

New

switch (string) {
    case "hi":
    case "bye":
        return "ciao";
    case "hello":
        return "salve";
    default:
        return "non capisco " + string;
}
                

Gotchas

No null checking - throws a NullPointerException

Case-sensitive

Don't forget to break; or return!

Try-With-Resources

Makes handling I/O streams and similar things easier

Gets rid of a lot of finally blocks

Pluggable due to new interface: AutoCloseable

Old I/O

InputStream is = null;
BufferedInputStream bs = null;
JsonReader json = null;
try {
    is = new FileInputStream("my-file.json");
    bs = new BufferedInputStream(is);
    json = new JsonReader(bs);
    //Do your stuff
} catch (IOException ex) {
    //handle it
} finally {
    try {
        if (json != null)
            json.close();
        if (bs != null)
            bs.close();
        if (is != null)
            is.close();
    } catch (IOException ex) {
        //Handle this exception
    }
}
            

With Try-With-Resources

try (
        InputStream is = new FileInputStream("my-file.json");
        BufferedInputStream bs = new BufferedInputStream(is);
        JsonReader json = new JsonReader(bs)
    ) {
    //Do your stuff
} catch (IOException ex) {
    //handle it
}
            

Extending

public class MyResource implements AutoCloseable {
    public void doSomethingResourceful() {
    }

    @Overrides
    public void close() {
        //close me
    }
}
            
try (MyResource mr = new MyResource()) {
    mr.doSomethingResourceful();
}
            

Combined Catch Blocks

Simplifies complicated try-catch blocks

Assume that jsonReader.read() throws a JsonException

try (
        InputStream is = new FileInputStream("my-file.json");
        BufferedInputStream bs = new BufferedInputStream(is);
        JsonReader json = new JsonReader(bs)
    ) {
    json.read();
} catch (IOException ex) {
    LOG.error("Error reading json", ex);
} catch (JSONException ex) {
    LOG.error("Error reading json", ex);
}
            

We could catch Exception, but what if there's some other exception that we want to let go?

try (
        InputStream is = new FileInputStream("my-file.json");
        BufferedInputStream bs = new BufferedInputStream(is);
        JsonReader json = new JsonReader(bs)
    ) {
    json.read();
} catch (SomeOtherException ex) {
    throw ex; //Propogate it up
} catch (Exception ex) {
    LOG.error("Error reading json", ex);
}
            

But what about runtime exceptions we don't know about?

Solution

try (
        InputStream is = new FileInputStream("my-file.json");
        BufferedInputStream bs = new BufferedInputStream(is);
        JsonReader json = new JsonReader(bs)
    ) {
    json.read();
} catch (IOException | JSONException ex) {
    LOG.error("Error reading json", ex);
}
            

Diamond Operator (<>)

Simplifies creating generic objects

//Old, verbose way
Map<String, String> java6 = new HashMap<String, String>();
//New, compact way
Map<String, String> java7 = new HashMap<>();
            

Enough with Java 7

Onward to Java 8!

Streams

Most of the big changes in Java 8 exist to make one thing possible: the Streams API

We're going to talk about Lambdas first, though.

THE END

- Try the online editor - Source code & documentation