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

Upgrade to Java 17

Upgrade to Java 17

Upgrade to Java 17 by Hao Cheng @ TWJUG 202203
Event: https://twjug.kktix.cc/events/twjug202203

2102a6b8760bd6f57f672805723dd83a?s=128

LINE Developers Taiwan
PRO

March 23, 2022
Tweet

More Decks by LINE Developers Taiwan

Other Decks in Programming

Transcript

  1. Upgrade to Java 17 haocheng 2022/03

  2. 01 02 03 04 05 06 Contents Upgrade to Java

    17 New features Toolchain Support Upgrade Issues Q&A References
  3. 介紹演講者 Hao Cheng Lee LINE Taiwan, Senior Engineer • Senior

    engineer @ LINE Taiwan EC • Co-organizer of TWJUG/JCConf • Interested in Java, JVM, Continuous Integration and TDD
  4. Why we want to upgrade to Java 17 • We

    can use all the new features added from Java 12 to Java 17 • We can utilize latest ZGC to have sub 1ms GC pause time • Java 11 will EOF on 2024/10 while Java 17 will be supported till 2026 • Java 17 has improved GC performance • Frameworks/libraries may require new Java version in the future
  5. New features • Switch Expression • Helpful NullPointerExceptions • Text

    Blocks • Record class • Pattern Matching for instanceof • Sealed class/interface
  6. Switch Expression • Switch expression is introduced in Java 14

    • Switch expressions evaluate to a single value • Use “case ->” in stead of “case:” • Use “yield” instead of “return” to return value • The cases of switch expressions must be exhaustive
  7. Switch Expression Support we have enum Day and we would

    like to return number of letters by different enum values
  8. Switch Expression Plain old Switch statement everyone is familiar with

  9. Switch Expression "case L ->" labels in Switch expression (no

    need to add break)
  10. Switch Expression "case L:” label need to use “yield” to

    return the value
  11. Switch Expression “case L ->” label also support multiple statements

  12. Switch Expression The cases of switch expressions must be exhaustive

  13. Helpful NullPointerExceptions • Helpful NullPointerExceptions is introduced in Java 14

    • Previously, NullPointerExceptions only show line number • NullPointerExceptions will show which variable is null now
  14. Helpful NullPointerExceptions Error messages would show which variable is null

  15. Text Blocks • Text Blocks is introduced in Java 15

    • Embed multi-line code snippets is a mess in previous version of Java • Text Blocks would eliminate all those obstructions • Text Blocks can be used anywhere a traditional double quote can be used
  16. Text Blocks

  17. Text Blocks A text block begins with three double-quote characters

    followed by a line terminator Text Blocks syntax
  18. Text Blocks What if we want to represent a multi-line

    string without that final \n? The final new line
  19. Text Blocks A text block would differentiate incidental white space

    from essential white space Incidental white space
  20. Text Blocks Preserve white spaces by shifting the content lines

    of the text block to the right and keeping the closing triple-quote delimiter Incidental white space
  21. Text Blocks Trailing white space on each line is also

    considered incidental and is stripped away. If we want to keep trailing space, we need to use one of the strategies listed Trailing white space
  22. Text Blocks A single space character is treated the same

    as a single tab character Detecting Potential Issues with White Space
  23. Text Blocks Java would normalizes all line terminators in a

    text block to be \n regardless of OS Normalization of line terminators
  24. Record Class • Record Class is introduced in Java 16

    (JEP395) • Record Class is a special kind of class to model plain data aggregates • accessors/constructor/equals/hashCode/toString are created automatically • All fields are final because it’s intended to serve as data carrier
  25. Record Class A record declaration specifies in a header a

    description of its contents; accessors, constructor, equals, hashCode, and toString methods are created automatically Record Class syntax
  26. Record Class We can declare a compact constructor whose signature

    is derived from the components automatically Compact constructor
  27. Record Class We need to make sure that our provided

    methods must have the same characteristics as implicitly derived accessors Explicit declaration of members
  28. Record Class A local record class is similar to a

    local class; it's a record class defined in the body of a method Local record class
  29. Record Class Intellij IDEA which would not show correct generated

    builder methods if we use Lombok @Builder with record class (This issue is fixed in 2022.1) Lombok @Builder
  30. Pattern Matching for instanceof • Pattern Matching for instanceof is

    introduced in Java 16 (JEP394) • Enables us to remove the conversion step • Pattern Matching for switch is still in preview (in Java 17)
  31. Pattern Matching for instanceof We need to cast the variable

    in previous instanceof
  32. Pattern Matching for instanceof Pattern Matching for instanceof would help

    to remove the cast step
  33. Sealed Class • Sealed Class is introduced in Java 17

    (JEP409) • Help to restrict which classes or interfaces may extend or implement • Permitted subclasses have the following constraints • Accessible by the sealed class at compile time • Directly extend the sealed class • Have exactly one of the following modifiers: • final: Cannot be extended further • non-sealed: Can be extended by unknown subclasses • sealed: Can only be extended by its permitted subclasses • In the same module/same package as the sealed class
  34. Sealed Class Permitted subclass must describe how it continues the

    sealing initiated by its superclass Constraints
  35. Sealed Class Sealed interface is also supported and record class

    can implement it just like normal class Sealed interface
  36. ZGC • ZGC is introduced as an experimental feature in

    Java 11 • ZGC is declared production ready in Java 15 • ZGC is a scalable low latency garbage collector designed to meet the goals: • Sub-millisecond max pause times • Pause times do not increase with the heap, live-set or root-set size • Handle heaps ranging from a 8MB to 16TB in size • Max throughput reduction < 15%
  37. ZGC The most important tuning option is setting the max

    heap size (-Xmx<size>) • The heap can accommodate the live-set of your application • Enough headroom in the heap to allow allocations to be serviced during GC • Use -XX:MaxRAMPercentage instead if VM has different spec Setting Heap Size
  38. ZGC The second option is number of concurrent GC threads

    (-XX:ConcGCThreads=<n>) • This option dictates how much CPU-time the GC should be given • Too much: the GC will steal too much CPU-time from the application • Too little: the application might allocate garbage faster than the GC can collect it Setting Concurrent GC Threads
  39. Toolchain Support • Spring Boot supports Java 17 from 2.5.5

    • Hibernate supports Java 17 from 5.3.22/5.4.32 • Lombok supports Java 17 from 1.8.21 • SonarQube supports Java 17 from 9.x (latest LTS is 8.9...)
  40. Upgrade Issues • AES/CTR/PKCS5Padding is not supported • 'MaxPermSize' option

    is removed • SonarQube does not support Java 17 in latest LTS
  41. Upgrade Issues Use 'AES/CTR/NoPadding' instead which is the same since

    'AES/CTR’ does not support padding (we were using AES/CTR/PKCS5Padding before to be backward-compatible with legacy code) AES/CTR/PKCS5Padding is not supported
  42. Upgrade Issues 'MaxPermSize' option is removed in Java 17, so

    adding this option would fail to create JVM in our Jenkins job 'MaxPermSize' option is removed
  43. Upgrade Issues SonarQube does not support Java 17 in latest

    LTS • SonarQube does not support Java 17 until 9.x • However, latest SonarQube LTS is 8.9 • Good news: SonarQube can still scan our codebase • Bad news: only 9.x support new features in Java 16/17
  44. Reference • Java 17 Language Updates • Java upgrade example

    errors and solutions • ZGC - The Next Generation Low-Latency Garbage Collector • GC progress from JDK 8 to JDK 17 • Why and How to Upgrade to Java 16 or 17 • How much faster is Java 17?
  45. THANK YOU