What's New in Java 8?

Feature Sets and Examples

By: Justin Dragos

@EllisandePoet

 

 

http://slides.com/justindragos/java-8/live

https://github.com/Ellisande/java-8-examples

The Shiny Stuff

  • New Time & Date APIs
  • Default Methods on Interfaces
  • Collections Improvements
  • Repeating Annotations
  • Method Parameter Reflections
  • Lambda Expressions
  • Method References
  • Security Improvements
  • Concurrency Improvements
  • Removal of PermGen

Date / Time APIs

New date/time APIs live in the java.time package

  • No more operating on longs to deal with time
  • (Joda Time)-like APIs for operations
  • Everything is immutable
  • Designed to be more easily testable
  • More intuitive classes
  • Based on the ISO calendar specs

The Local News

Java 8 introduces local times that don't hold a UTC offset. Since they don't hold a UTC offset these cannot be converted into seconds since the epoch (java.util.Date)

 

  • LocalTime – 10:15:30
  • LocalDate - 2007-12-03
  • LocalDateTime –  2007-12-03T10:15:30

I'm Slightly Offset

Offsets and instants are much closer foundationally to java.util.Date as they all represent an absolute point in time.

 

  • Instant – represents seconds since the epoch
  • OffsetTime – 10:15:30+0100
  • OffsetDateTime – 2007-12-03T10:15:30+0100

Span-dex

We also now have a concept of spans of time. This can be represented in two ways:

  • Duration – a time based amount, like 3.5 seconds
  • Period – a date based amount of time like 2 years, 3 months, and 4 days.

The Other Mentionables

  • Clock - allows you to change what now() returns
  • Year - simple year object (no messing with calendar)
  • YearMonth
  • ZoneId - human readable time zones 
Clock marchFifth = Clock.fixed(Instant.now(), ZoneId.of("America/Phoenix"));

Date/Time Examples

TimeTest.java

Bravely Default

Multiple Inheritance finally comes to java! Interfaces can now have default implementations of their methods. This allows you to:

  • Add functionality to old interfaces without breaking
  • Use methods to create compositional mixins
  • Add robust methods to functional interfaces 
  • Be very confused about where methods are coming from

Collection Amplification

Since collections no longer has to worry about breaking all Java code ever written, they enhanced the APIs!

  • Steaming APIs
  • Spliterators
  • Merge Functions
  • Lots of Map Improvements

Diamonds Are Forever

Ball

int bounceHeight(int)

BeachBall

int bounceHeight(int)

HardBall

int bounceHeight(int)

LeadBeachBall

bounceHeight?

Default Method Examples

DefaultMethodImplementationTest.java

I Love My Reflection

  • Multiple Annotations
  • Method Parameter Reflection
  • Pluggable Type Checkers

Reflection Examples

Car.java

New.java

News.java

Old.java

Volume.java

AnnotationTest.java

The Closure Disclosure

Closures come to Java! … Mostly.

 

A lambda expression is an anonymous method implementation for a functional interface.

 

So… that’s weird for Java, what do those things mean?

Let's Get Functional

A functional interface is an interface that has one and only one method without a default implementation (i.e. one truly abstract method)

@FunctionalInterface
public interface Filterable<T> {
    void filter(T item);
}
@FunctionalInterface
public interface Filterable<T> {
    void filter(T item);

    default T filter(T item){
        filter(item);
        return item;
    }
}

Lambda's Anatomy

Alright, lets break it down:



(sayWhat) -> { System.out.println("I said "+ sayWhat); }

(sayWhat) : names the input params for the lambda expression

-> : separates the parameter declaration from the method body

{ ... } : the body of the lambda expression

Lambda Examples

  • CallBacker.java
  • Filterable.java
  • IntToStringTransformer.java
  • MutableCar.java
  • PrintValues.java
  • LambdaTest.java

The Other Stuff

  • Concurrency Improvements
    • Collections support for functional concurrency
    • New lock and future classes
  • Security Updates
    • Stronger TLS
    • Kerberos 5
    • Stronger password based encryption
  • PermGen is gone!
  • Nashorn JavaScript Engine - Run JavaScript natively!

That's All Folks!

Thanks for attending!

Have any questions or comments?

 

If you are interested in opportunities with State Farm, or just want to know more about us, please stop by our booth in the cafeteria or talk to me after the presentation.

 

 

Github: http://github.com/Ellisande/java-8-examples

Twitter: @EllisandePoet

 

Made with Slides.com