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

Novedades en Java 16 (y repaso de Java 9 a 15) ...

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.

Jorge Hidalgo

June 17, 2021
Tweet

More Decks by Jorge Hidalgo

Other Decks in Programming

Transcript

  1. 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!
  2. JAVA 2021 Copyright © 2021 Jorge Hidalgo. All rights reserved.

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

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

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

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

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

    reserved. 11 OPEN SOURCE COMMUNITY-DRIVEN BACKWARDS COMPATIBLE
  8. 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.”
  9. THE NEW AND IMPROVED JAVA Copyright © 2021 Jorge Hidalgo.

    All rights reserved. 16 POLYGLOT (MORE THAN EVER)
  10. 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
  11. 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
  12. 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
  13. 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.)
  14. 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
  15. 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)
  16. 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
  17. LOCAL-VARIABLE TYPE INFERENCE Copyright © 2021 Jorge Hidalgo. All rights

    reserved. 26 before List<String> list = new ArrayList<>(); after var list = new ArrayList<String>();
  18. 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"); }
  19. 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; };
  20. 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);
  21. 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 }
  22. 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());
  23. 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"); } } }
  24. 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"); } } }
  25. 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; } }
  26. 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); } }