Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Ins and Outs of JRE 8 - 1DevDay 2014

Avatar for Dan Putman Dan Putman
November 15, 2014

The Ins and Outs of JRE 8 - 1DevDay 2014

Overview of the new features in Java 8 given at 1DevDay 2014 in Detroit

Avatar for Dan Putman

Dan Putman

November 15, 2014

Other Decks in Technology

Transcript

  1. Overview   •  How  did  we  get  here?   • 

    New  language  features   •  Lambdas   •  Streams   •  Nashorn   •  Date/Time   •  RepeaEng  AnnotaEons   •  Should  I  switch?   •  QuesEons,  Answers,  Complaints  
  2. How  did  we  get  here?   •  Key  milestones  in

     Java   •  Java  1.2  -­‐  JIT   •  Java  5  -­‐  Generics   •  Java  7  –  Support  for  dynamic  languages   •  C#  and  .NET   •  LINQ   •  Implicit  typing  (var)   •  The  rise  of  dynamic  languages   •  Ruby   •  Scala,  Closure,  Groovy   •  Node.js  
  3. Button btn = new Button(); Btn.setText(“Hello World”); btn.setOnAction( new EventHandler<ActionEvent>()

    { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); panel.add(btn); …
  4. Button btn = new Button(); Btn.setText(“Hello World”); btn.setOnAction( (event) ->

    System.out.println("Hello World!”) ); panel.add(btn); …
  5. Lambdas   •  Stateless  blocks  of  code  that  can  be

     executed   at  later  Eme   •  Good  riddance  to  anonymous  classes   •  Makes  funcEonal  programming  much  easier   •  Enables  wriEng  of  clear,  concise  code   •  Brings  Java  into  the  modern  era  
  6. IntConverter f1 = new IntConverter() { @Override public int convert(int

    x) { return x * 2; } }; IntConverter f2 = (x) -> {return x * 3;}; f1.convert(10); f2.convert(10);
  7. Lambdas  –  FuncEonal  Interfaces   •  Predefined  interfaces  used  to

     define  lambda   targets   •  Used  by  the  JDK,  but  can  be  inserted  into   user  code   •  Types   •  Consumer   •  Predicate   •  Supplier   •  Operator   •  FuncEon  
  8. public int process(IntConverter converter){ return converter.convert(10); } … IntConverter c

    = (x) -> {return x*3;}; process(c); … Custom  Interface  
  9. public static int process(IntUnaryOperator lambda){ return lambda.applyAsInt(10); } public static

    void main(String[] args) { process( (x) -> x*3 ); } FuncEonal  Interface  
  10. Streams   •  NOT  an  IOStream!   •  A  sequence

     of  elements  supporEng   sequenEal  and  parallel  aggregate  operaEons   •  Allows  programmers  to  write  a  pipeline  of     filters,  conversions,  and  other  operaEons   followed  by  a  terminal  operaEon  
  11. int sum = widgets.stream() .filter(w -> w.getColor() == RED) .mapToInt(w

    -> w.getWeight()) .sum(); Sum  of  Red  Widget  Weights  
  12. List<String> strings = Arrays.asList( "cat", "apple", "dog", "car", "house"); strings

    .stream() .filter(s -> s.startsWith("c")) .map(String::toUpperCase) .sorted() .forEach(System.out::println); Transform,  sort,  and  print  words   starEng  with  C    
  13. List<String> strings = Arrays.asList( "cat", "apple", "dog", "car", "house"); strings

    .stream() .filter(s -> s.startsWith("c")) .map(s -> s.toUpperCase()) .sorted() .forEach(s -> System.out.println(s)); Transform,  sort,  and  print  words   starEng  with  C    
  14. Intermediate  FuncEons   •  filter   •  map   • 

    flatMap   •  sorted   •  skip   •  limit   •  disEnct  
  15. Terminal  FuncEons   •  sum   •  reduce   • 

    count   •  findFirst   •  forEach   •  allMatch   •  anyMatch  
  16. StaEc  FuncEons   •  generate   •  iterate   • 

    range       IntStream.iterate(0, n -> n + 3).limit(3) .boxed().collect(Collectors.toList());
  17. Keys  to  Remember   •  Streams  are  lazy!   • 

    They  will  not  execute  unEl  a  terminal   operaEon  is  called   •  Order  of  operaEons  mafers!   •  Filter  -­‐>  Map  !=  Map  -­‐>  Filter   •  Order  of  elements  should  not  mafer!   •  Don’t  expect  elements  to  be  processed  in   the  order  added  
  18. List<String> strings = Arrays.asList( "cat", "apple", "dog", "car", "house"); strings

    .stream() .filter(s -> s.startsWith("c")) .map(s -> s.toUpperCase()) .sorted() .forEach(s -> System.out.println(s));
  19. List<String> strings = Arrays.asList( "cat", "apple", "dog", "car", "house"); strings

    .parallelStream() .filter(s -> s.startsWith("c")) .map(s -> s.toUpperCase()) .sorted() .forEach(s -> System.out.println(s)); Parallel  Streams!  
  20. Nashorn  –  The  new  Rhino   •  First  and  foremost

     a  Javascript  engine   •  Built  to  replace  Rhino   •  Can  be  called  from  the  command  line,  similar   to  java  or  javac     Hello.js  -­‐>    print("Hello  World!");     $  jjs  hello.js  -­‐>    Hello  World!  
  21. ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("nashorn"); engine.eval(

    "function sum(a, b) { return a + b; }"); System.out.println( engine.eval("sum(1, 2);")); //Output is 3 Nashorn  –  Also  embeddable  
  22. public interface Adder { int sum(int a, int b); }

    Nashorn  –  Using  values  
  23. public interface Adder { int sum(int a, int b); }

    Invocable invocable = (Invocable) engine; Adder adder = invocable.getInterface(Adder.class); Nashorn  –  Using  values  
  24. public interface Adder { int sum(int a, int b); }

    Invocable invocable = (Invocable) engine; Adder adder = invocable.getInterface(Adder.class); int sum = adder.sum(2, 3); System.out.println(sum + 10); // Output is 15 Nashorn  –  Using  values  
  25. Nashorn  –  Other  Tricks   •  Load  and  evaluate  enEre

     javascript  files   •  This  includes  libraries  like  Mustache,  Lo-­‐ dash,  etc     •  Interact  with  Java  code  and  types  within   Javascript   •  Extend  Java  classes  from  Javascript    
  26. Nashorn  –  Other  Tricks   •  Load  and  evaluate  enEre

     javascript  files   •  This  includes  libraries  like  Mustache,  Lo-­‐ dash,  etc     •  Interact  with  Java  code  and  types  within   Javascript   •  Extend  Java  classes  from  Javascript   •  A  mulEtude  of  things  that  should  frighten  you  
  27. Default  Methods   •  Allows  the  declaraEon  of  a  default

      implementaEon  on  an  interface   •  ImplemenEng  classes  can  do  3  things   •  Do  nothing  and  inherit  the  default   •  Re-­‐declare  the  method  to  make  it  abstract   •  Redefine  the  method  to  override  the   behavior  
  28. Default  Methods   •  Allows  the  declaraEon  of  a  default

      implementaEon  on  an  interface   •  ImplemenEng  classes  can  do  3  things   •  Do  nothing  and  inherit  the  default   •  Re-­‐declare  the  method  to  make  it  abstract   •  Redefine  the  method  to  override  the   behavior   •  StaEc  methods  are  also  allowed  on  interfaces  
  29. public class SimpleEmployee implements Employee{ private String firstName; private String

    lastName; private String id; @Override public String getFirstName() { return firstName; } … Default  Methods  -­‐  Class  
  30. public interface Employee { String getFirstName(); String getLastName(); long getId();

    String getFullName(); } Default  Methods  -­‐  Interface  
  31. public interface Employee { String getFirstName(); String getLastName(); long getId();

    default String getFullName(){ return getFirstName()+ getLastName(); } } Default  Methods  -­‐  Interface  
  32. Date/Time  API   •  Why  do  we  need  a  new

     Date/Time  API?   •  Classes  like  java.uEl.Date  were  not       thread-­‐safe   •  Months  start  at  1,  Days  at  0   •  Everyone  was  using  Joda-­‐Time  anyway   •  So  Java  stole  the  good  stuff   •  Immutable-­‐value  classes   •  DDD  (Domain  Driven  Design)  API  
  33. Date/Time  Keys   •  DateTime  and  TimeZones  are  separated  

    •  LocalDateTime  is  now  the  base  class  most   programmers  will  interact  with   •  LocalDateTime,  LocalDate,  and  LocalTime   are  all  disEnct  enEEes   •  TemporalAdjusters  make  common  tasks  easy   •  First  or  last  day  of  the  month   •  First  or  last  day-­‐of-­‐week  in  the  month   •  Next  day-­‐of-­‐week  
  34. RepeaEng  AnnotaEons   •  Prior  to  now,  developers  could  only

     apply  a   single  annotaEon  of  a  given  type   •  Could  work  around  this  by  creaEng  a  wrapper   annotaEon   •  Was  quite  clunky   •  RepeaEng  AnnotaEons  hides  the  “clunkiness”    
  35. @Repeatable(value = Routes.class) public @interface Route { String value() default

    ""; } public @interface Routes { Route[] value(); } RepeaEng  AnnotaEons  
  36. Support   •  IDEs   •  Eclipse  Luna   • 

    Intellij  IDEA  13.1   •  NetBeans  7.4  and    8   •  Frameworks   •  Spring  4   •  PlayFramework  2.3.5+   •  Spark   •  Servers   •  Tomcat  8   •  Jefy  9  
  37. Performance   •  The  Good   •  Most  benchmarks  have

     it  even  or  slightly   ahead  of  Java  7   •  Ater  JVM  warmup,  streams  can  perform   befer  than  comparable  imperaEve  code   •  Lambdas  out  perform  inner  classes  
  38. Performance   •  The  Good   •  Most  benchmarks  have

     it  even  or  slightly   ahead  of  Java  7   •  Ater  JVM  warmup,  streams  can  perform   befer  than  comparable  imperaEve  code   •  Lambdas  out  perform  inner  classes   •  The  Bad   •  Parallel  streams  can  kill  scalability  if  you’re   not  careful   •  ImperaEve  loops  are  sEll  king  in  some   situaEons  
  39. Reasons  not  to  upgrade   •  ……   •  No,

     really.  I  can’t  think  of  any    
  40. Reasons  not  to  switch     •  Streams  and  lambdas

     can  someEmes  make   stack  traces  nasty   •  FuncEonal  programming  isn’t  fully  baked   •  Scala,  Groovy,  and  others  sEll  do  it  befer   •  Performance  can  be  slighty  slower  if   programmers  aren’t  careful  with  streams        
  41. So  what  does  this  all  mean?   •  Java  is

     pulling  in  the  best  ideas  from  the   community  and  catching  up  to  other  modern   languages   •  There  is  a  lot  of  depth  to  this  update.  We  sEll   don’t  know  what  the  community  will  do  with   it   •  No  single  programming  paradigm  is  going  to   be  good  enough  anymore  
  42. The  Future   •  Java  9  –  2016   • 

    Project  Jigsaw   •  Money  and  Currency  API   •  AutomaEc  ParallelizaEon      
  43. The  Future   •  Java  9  –  2016   • 

    Project  Jigsaw   •  Money  and  Currency  API   •  AutomaEc  ParallelizaEon   •  Java  10  –  2018…    
  44. The  Future   •  Java  9  –  2016   • 

    Project  Jigsaw   •  Money  and  Currency  API   •  AutomaEc  ParallelizaEon   •  Java  10  –  2018…   •  Objects  without  value  types   •  64-­‐bit  addresses  for  large  datasets  
  45. Notes  –  DELETE!   -­‐Lambdas   -­‐-­‐Concise  code   -­‐-­‐Internal

     iteraEon  (serial  vs  parallel)   -­‐-­‐Asynchronous  calls   -­‐-­‐Play  framework  comparison   -­‐-­‐Map  Reduce  +  Chaining   -­‐Date  and  Time   -­‐Performance   -­‐Nashorn   -­‐RepeaEng  AnnotaEons?   -­‐Default  Methods   -­‐Eclipse  and  IntelliJ  integraEon   -­‐Arugments  against