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

Novedades en Java 16 (y repaso de Java 9 a 15) @ MálagaJUG 2021-06-17

Novedades en Java 16 (y repaso de Java 9 a 15) @ MálagaJUG 2021-06-17

Slides de la presentación sobre novedades en Java 16 (y repaso de 9 a 15) realizada para MálagaJUG el 17 de junio de 2021.

D964b48ba3c3366cc967250087401257?s=128

Jorge Hidalgo

June 17, 2021
Tweet

Transcript

  1. NOVEDADES EN JAVA 16 JORGE HIDALGO 17 JUNIO 2021 (Y

    REPASO DE JAVA 9 A 15)
  2. WHO AM I? JORGE HIDALGO Senior Manager – Senior Technology

    Architect at Accenture Global Java Lead / Spain DevOps Lead / Advanced Technology Center in Spain – Software Engineering Lead MálagaJUG Co-lead Father of two, husband, whistle player, video gamer, sci-fi *.*, Lego, LOTR, Star Wars, Star Trek, Starcraft, Destiny, Halo, Borderlands, Raspberry Pi, Arduino… LLAP!
  3. JAVA 2021 Copyright © 2021 Jorge Hidalgo. All rights reserved.

    3
  4. JAVA 2021 Copyright © 2021 Jorge Hidalgo. All rights reserved.

    4 JAVA IS STILL HERE AND WELL ALIVE
  5. JAVA 2021 Copyright © 2021 Jorge Hidalgo. All rights reserved.

    5 TIOBE REDMONK PYPL OCTOVERSE
  6. JAVA 2021 Copyright © 2021 Jorge Hidalgo. All rights reserved.

    6 JAVA IS STILL HERE AND WELL ALIVE Duke
  7. WE WANTED MORE Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 7 JAVA (BOTH AS PLATORM AND AS LANGUAGE) WAS NOT MOVING FORWARD FAST ENOUGH
  8. WE WANTED MORE Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 8 LONG RELEASE CADENCE SLOW INNOVATION PACE BLAME LANGUAGE & PLATFORM FOR OLD APIS
  9. WE WANTED MORE Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 9 DISRUPTED BY THE CLOUD -- NOT DISRUPTIVE ENTERPRISE PLATFORM LOSING RELEVANCE
  10. BUT… WAIT! Copyright © 2021 Jorge Hidalgo. All rights reserved.

    10 WE SHOULD MIGRATE OUR CODE TO JAVASC…. NO! THERE IS STILL HOPE WITH JAVA!
  11. THE RIGHT STUFF Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 11 OPEN SOURCE COMMUNITY-DRIVEN BACKWARDS COMPATIBLE
  12. JAMES GOSLING’S FAMOUS 2011 QUOTE Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 12 “At the core of the Java ecosystem is the JVM. Most people talk about Java the language, and this may sound odd coming from me, but I could hardly care less. What I really care about is the Java Virtual Machine as a concept, because that is the thing that ties it all together.”
  13. JAVA REACTED Copyright © 2021 Jorge Hidalgo. All rights reserved.

    13
  14. THE NEW AND IMPROVED JAVA Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 14
  15. THE NEW AND IMPROVED JAVA Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 15 MODULAR
  16. THE NEW AND IMPROVED JAVA Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 16 POLYGLOT (MORE THAN EVER)
  17. THE NEW AND IMPROVED JAVA Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 17 LIGHTWEIGHT
  18. THE NEW AND IMPROVED JAVA Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 18 FASTER
  19. NOTES FOR THE UNWARY JAVA DEVELOPER Copyright © 2021 Jorge

    Hidalgo. All rights reserved. 19 6 MONTH RELEASE CYCLE LTS RELEASE EVERY 3 YEARS CURRENT LTS JAVA 11 NEXT WILL BE JAVA 17
  20. NOTES FOR THE UNWARY JAVA DEVELOPER Copyright © 2021 Jorge

    Hidalgo. All rights reserved. 20 MANY JDK BUILDS TO CHOOSE FROM: ORACLE RED HAT IBM SAP (SAPMACHINE) MICROSOFT AZUL ZULU (& ZING) AMAZON CORRETTO ADOPTOPENJDK
  21. JAVA 9 TO 15 – MAIN NEW FEATURES Copyright ©

    2021 Jorge Hidalgo. All rights reserved. 21 Java 9 Modules Interface private methods JShell Java 10 Local variable type inference (var) Container awareness
  22. JAVA 9 TO 15 – MAIN NEW FEATURES Copyright ©

    2021 Jorge Hidalgo. All rights reserved. 22 Java 11 String enhancements Local-variable for Lambda params. Flight Recorder is open source Z Garbage Collector (experimental) Java 12 Enhanced switch Shenandoah low-pause GC (exp.)
  23. JAVA 9 TO 15 – MAIN NEW FEATURES Copyright ©

    2021 Jorge Hidalgo. All rights reserved. 23 Java 13 String text blocks (preview) Switch expressions (preview) Java 14 Switch expressions (final) Records (preview) Pattern matching for instanceof (preview) Enhanced NullPointerException
  24. JAVA 9 TO 15 – MAIN NEW FEATURES Copyright ©

    2021 Jorge Hidalgo. All rights reserved. 24 Java 15 String text blocks (final) Sealed classes (preview) Records (second preview) Pattern matching for instanceof (second preview) Hidden classes Z Garbage Collector (prod. ready) Shenandoah GC (prod. ready)
  25. JAVA 16 – NEW AND NOTEWORTHY Copyright © 2021 Jorge

    Hidalgo. All rights reserved. 25 Java 16 (full list here) Records (final) Pattern matching for instanceof (final) Vector API (preview) Sealed classes (second preview) Static members in inner classes Stream.toList() Migration from Mercurial to Git Windows ARM64 & Alpine Ports
  26. LOCAL-VARIABLE TYPE INFERENCE Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 26 before List<String> list = new ArrayList<>(); after var list = new ArrayList<String>();
  27. ENHANCED SWITCH & EXPRESSIONS Copyright © 2021 Jorge Hidalgo. All

    rights reserved. 27 before int days = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: days = 30; break; case 2: days = 28; break; default: throw new IllegalArgumentException("Invalid month"); }
  28. ENHANCED SWITCH & EXPRESSIONS Copyright © 2021 Jorge Hidalgo. All

    rights reserved. 28 after int days = switch (month) { case 1, 3, 5, 7, 8, 10, 12 -> 31; case 4, 6, 9 -> 30; case 2 -> 28; default -> 0; }; or int days = switch (month) { case 1, 3, 5, 7, 8, 10, 12 -> 31; case 4, 6, 9 -> 30; case 2 -> { System.out.println("check the year!"); yield 28; } default -> 0; };
  29. STRING TEXT BLOCKS Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 29 var dbQuery = """ CREATE TABLE 'TEST'.'EMPLOYEE' ( 'ID' INT NOT NULL DEFAULT 0, 'FIRST_NAME' VARCHAR(100) NOT NULL, 'LAST_NAME' VARCHAR(100) NULL, 'STAT_CD' TINYINT NOT NULL DEFAULT 0 ); """; System.out.println(dbQuery);
  30. RECORDS Copyright © 2021 Jorge Hidalgo. All rights reserved. 30

    before public class AuditInfo { private final LocalDateTime createdOn; private final String createdBy; private final LocalDateTime updatedOn; private final String updatedBy; public AuditInfo(LocalDateTime createdOn, String createdBy, LocalDateTime updatedOn, String updatedBy) { this.createdOn = createdOn; this.createdBy = createdBy; this.updatedOn = updatedOn; this.updatedBy = updatedBy; } public LocalDateTime getCreatedOn() { return createdOn; } // rest of getters, setters, equals, hashCode }
  31. RECORDS Copyright © 2021 Jorge Hidalgo. All rights reserved. 31

    after public record AuditInfo( LocalDateTime createdOn, String createdBy, LocalDateTime updatedOn, String updatedBy) { } how to use it var rec = new AuditInfo( LocalDateTime.now().minusHours(1), "jorge", LocalDateTime.now(), "paco"); System.put.println(rec.updatedBy());
  32. PATTERN MATCHING FOR INSTANCEOF Copyright © 2021 Jorge Hidalgo. All

    rights reserved. 32 before public interface Shape { public static double getPerimeter( Shape shape) throws IllegalArgumentException { if (shape instanceof Rectangle) { Rectangle r = (Rectangle) shape; return 2 * r.length() + 2 * r.width(); } else if (shape instanceof Circle) { Circle c = (Circle) shape; return 2 * c.radius() * Math.PI; } else { throw new IllegalArgumentException( "Unrecognized shape"); } } }
  33. PATTERN MATCHING FOR INSTANCEOF Copyright © 2021 Jorge Hidalgo. All

    rights reserved. 33 after public interface Shape { public static double getPerimeter( Shape shape) throws IllegalArgumentException { if (shape instanceof Rectangle r) { return 2 * r.length() + 2 * r.width(); } else if (shape instanceof Circle c) { return 2 * c.radius() * Math.PI; } else { throw new IllegalArgumentException( "Unrecognized shape"); } } }
  34. VECTOR API Copyright © 2021 Jorge Hidalgo. All rights reserved.

    34 before void scalarComputation( float[] a, float[] b, float[] c) { for (int i = 0; i < a.length; i++) { c[i] = (a[i] * a[i] + b[i] * b[i]) * -1.0f; } }
  35. VECTOR API Copyright © 2021 Jorge Hidalgo. All rights reserved.

    35 after (full example in GitHub here) static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED; void vectorComputation( float[] a, float[] b, float[] c) { for (int i = 0; i < a.length; i += SPECIES.length()) { var mask = SPECIES.indexInRange(i, a.length); var va = FloatVector.fromArray( SPECIES, a, i, mask); var vb = FloatVector.fromArray( SPECIES, b, i, mask); va.pow(2).add(vb.pow(2)).neg().intoArray( c, i, mask); } }
  36. YOU SHALL ASK!