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

Refactoring to Java 21

Refactoring to Java 21

Presented at Philly Java User Group

Jeanne Boyarsky

August 23, 2023
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Programming

Transcript

  1. @jeanneboyarsky mastodon.social/@jeanneboyarsky 1 Refactoring to Java 21 and beyond Jeanne

    Boyarsky August 23, 2023 Philly JUG speakerdeck.com/boyarsky
  2. @jeanneboyarsky mastodon.social/@jeanneboyarsky Version of Java? 5 Version Plan 7 Upgrade

    ASAP. Even extended support is over. 8 or 11 Align code to future. “older Java comments” 9-10, 12-16, 18-19 Upgrade; not on latest LTS 17 Lots of refactoring 20-21 Even more refactoring
  3. @jeanneboyarsky mastodon.social/@jeanneboyarsky For each Topic • Example • About the

    feature • Opportunities • IDE Support • What to do if on older Java 6
  4. @jeanneboyarsky mastodon.social/@jeanneboyarsky Example: REST API Params public String getJson(String search)

    { String json = "{" + " \"query\": \"%s\"" + " \"start\": \"1\"," + " \"end\": \"10\"" + "}"; return String.format(json, search); } 9 This is hard to read
  5. @jeanneboyarsky mastodon.social/@jeanneboyarsky Take Two public String getJson(String search) { Path

    path = Path.of( “src/main/resources/query.json"); String json = null; try { json = Files.readString(path); } catch (IOException e) { throw new UncheckedIOException(e); } return String.format(json, search); } 10 Now the String is far away
  6. @jeanneboyarsky mastodon.social/@jeanneboyarsky Text Block public String getJson(String search) { String

    json = """ { "query": "%s" "start": "1" "end": "10" }"""; return String.format(json, search); } 11 It’s a string literal! 15 Adds line breaks, but still works
  7. @jeanneboyarsky mastodon.social/@jeanneboyarsky Text Block Syntax String textBlock = """ kcdc,Kansas

    City,"session,workshop" meetup,Various,lecture """; incidental whitespace start block end block 15 12
  8. @jeanneboyarsky mastodon.social/@jeanneboyarsky Essential Whitespace String textBlock = """ <session> <speaker>

    Jeanne Boyarsky </speaker> </session> """; incidental whitespace essential whitespace 15 13
  9. @jeanneboyarsky mastodon.social/@jeanneboyarsky Ending lines String textBlock = """ <session> <speaker>

    Jeanne Boyarsky \s </speaker> <title> Becoming one of the first Java 17 \ certified programmers \ (and learning new features) </title> </session> """; continue on next line without a line break new escape character keeps trailing whitespace tab 15 14
  10. @jeanneboyarsky mastodon.social/@jeanneboyarsky New lines String textBlock = """ <session>\n <speaker>

    Jeanne\nBoyarsky </speaker> </session>"""; no line break at end Two new lines (explicit and implicit) One new line (explicit) 15 15
  11. @jeanneboyarsky mastodon.social/@jeanneboyarsky IDE Support 18 String json = """ {

    "query": "%s" "start": "1", "end": "10"}"""; Literal refactoring - no \n
  12. @jeanneboyarsky mastodon.social/@jeanneboyarsky IDE Support 19 String json = """ {\

    "query": "%s"\ "start": "1",\ "end": "10"\ }"""; Preserve lines but still no \n
  13. @jeanneboyarsky mastodon.social/@jeanneboyarsky On older Java? public String getJson(String search) {

    //TODO convert to text block when on Java 17 String json = "{" + " \"query\": \"%s\"" + " \"start\": \"1\"," + " \"end\": \"10\"" + "}"; return String.format(json, search); } 20 Hard to read but positions for future
  14. @jeanneboyarsky mastodon.social/@jeanneboyarsky Java 5+ Options /** * <pre> * List&lt;String&gt;

    getNames() { * * } * </pre> * * <code>List&lt;String&gt; getNames() { } </code> * * @code List<String> getNames {}</String>} */ 22
  15. @jeanneboyarsky mastodon.social/@jeanneboyarsky Combined /** * <pre>@code * List<String> getNames() {

    * * } * }</pre> */ 23 No HTML IDE limitations: recognize, syntax highlighting, etc
  16. @jeanneboyarsky mastodon.social/@jeanneboyarsky Inline Snippet /** * {@snippet : * List<String>

    getNames() { * * } * } */ 24 18 Limitations: No /* */ comments Must have balanced pairs of {}
  17. @jeanneboyarsky mastodon.social/@jeanneboyarsky Extended Snippet /** * {@snippet file=“JavaClass.java" region="section"} */

    // @start region=“section" /* many lines */ // @end 25 20 Real files editable in your IDE
  18. @jeanneboyarsky mastodon.social/@jeanneboyarsky On older Java? //TODO switch to snippet on

    Java 21 /** * <pre>@code * List<String> getNames() { * * } * }</pre> */ 27 Positions for future
  19. @jeanneboyarsky mastodon.social/@jeanneboyarsky On older Java? //TODO use external snippet on

    Java 21 using ThisClass.java /** * <pre>@code * List<String> getNames() { * * } * }</pre> */ // @start region=“section" // @end 28
  20. @jeanneboyarsky mastodon.social/@jeanneboyarsky Record 16 public record Book (String title, int

    numPages) { } New type Automatically get * final record * private final instance variables * public accessors * constructor taking both fields * equals * hashCode * toString 31
  21. @jeanneboyarsky mastodon.social/@jeanneboyarsky Using the Record 16 Book book = new

    Book("Breaking and entering", 289); System.out.println(book.title()); System.out.println(book.toString()); No “get” Outputs: Breaking and entering Book[title=Breaking and entering, numPages=289] 32
  22. @jeanneboyarsky mastodon.social/@jeanneboyarsky Opportunities •Immutable POJOs •Don’t have to write equals/

    hashCode •Vs reflection - EqualsBuilder •Make code coverage tool happy •Others? 17 33
  23. @jeanneboyarsky mastodon.social/@jeanneboyarsky IDE Support 34 public record Book(String title, int

    numPages) { } Had to make instance variables final. Also didn’t remove my equals() even though generated by IntelliJ
  24. @jeanneboyarsky mastodon.social/@jeanneboyarsky On older Java? //TODO convert to record when

    on Java 17 public final class Book { private String title; private int numPages; public Book(String title, int numPages) { this.title = title; this.numPages = numPages; } public String title() { return title; } public int numPages() { return numPages; } // hash code, equals 35 Be sure to use al fields for equals/ hashCode
  25. @jeanneboyarsky mastodon.social/@jeanneboyarsky Casting if (num instanceof Integer) { Integer numAsInt

    = (Integer) num; System.out.println(numAsInt); } if (num instanceof Double) { Double numAsDouble = (Double) num; System.out.println(numAsDouble.intValue()); } 37
  26. @jeanneboyarsky mastodon.social/@jeanneboyarsky Casting 16 if (num instanceof Integer numAsInt) {

    System.out.println(numAsInt); } if (num instanceof Double numAsDouble) { System.out.println(numAsDouble.intValue()); } Pattern variable 38
  27. @jeanneboyarsky mastodon.social/@jeanneboyarsky Flow Scope 16 if (num instanceof Double d1

    && d1.intValue() % 2 == 0) { System.out.println(d1.intValue()); } if (num instanceof Double d2 || d2.intValue() % 2 == 0) { System.out.println(d2.intValue()); } Does not compile because d2 might not be double Compiles 39
  28. @jeanneboyarsky mastodon.social/@jeanneboyarsky Does this compile? 16 if (num instanceof Double

    n) System.out.println(n.intValue()); if (num instanceof Integer n) System.out.println(n); Yes. Only in scope for if statement 40
  29. @jeanneboyarsky mastodon.social/@jeanneboyarsky Does this compile? 16 if (num instanceof Double

    n) System.out.println(n.intValue()); System.out.println(n.intValue()); No. If statement is over 41
  30. @jeanneboyarsky mastodon.social/@jeanneboyarsky Does this compile? 16 if (!(num instanceof Double

    n)) { return; } System.out.println(n.intValue()); Yes. Returns early so rest is like an else 42
  31. @jeanneboyarsky mastodon.social/@jeanneboyarsky Does this compile? 16 if (!(num instanceof Double

    n)) { return; } System.out.println(n.intValue()); if (num instanceof Double n) System.out.println(n.intValue()); No. n is still in scope 43
  32. @jeanneboyarsky mastodon.social/@jeanneboyarsky Record Pattern 21 public static void titles(List<Object> books)

    { for (var obj : books) { if (obj instanceof Book(String title, int numPages)) { System.out.println(title); } } } 44
  33. @jeanneboyarsky mastodon.social/@jeanneboyarsky On older Java? //TODO convert to pattern var

    when on Java 17 if (num instanceof Double) { Double numAsDouble = (Double) num; System.out.println(numAsDouble.intValue()); } 47 Positions for future
  34. @jeanneboyarsky mastodon.social/@jeanneboyarsky Originally public String getLocation(String store) { String result

    = ""; switch (store) { case "Hallmark": result = "KC"; break; case "Crayola": result = "PA"; break; default: result = "anywhere"; } return result; } You remembered the breaks, right? 49
  35. @jeanneboyarsky mastodon.social/@jeanneboyarsky Switch Expressions 14 public String getLocation(String store) {

    return switch (store) { case "Hallmark" -> "KC"; case "Crayola" -> "PA"; default -> "anywhere"; }; } Arrow labels No break keyword 50
  36. @jeanneboyarsky mastodon.social/@jeanneboyarsky Missing value 14 enum Position { TOP, BOTTOM

    }; Position pos = Position.TOP; int stmt = switch(pos) { case TOP: yield 1; }; int expr = switch(pos) { case BOTTOM -> 0; }; Does not compile because assigning value (poly expression) 51
  37. @jeanneboyarsky mastodon.social/@jeanneboyarsky Pattern matching for switch 21 public int toInt(Object

    obj) { return switch (obj) { case Integer i -> i; case Double d -> d.intValue(); case String s -> Integer.parseInt(s); default -> throw new IllegalArgumentException("unknown type"); }; } Reminder: Syntax can change 52
  38. @jeanneboyarsky mastodon.social/@jeanneboyarsky But wait, there’s more 21 static void printOddOrEven(Object

    obj) { switch (obj) { case Integer i when i % 2 == 1 -> System.out.println("odd"); case Integer i when i % 2 == 0 -> System.out.println(“even"); default -> System.out.println("not an int"); }; } Reminder: Feature can still change 53
  39. @jeanneboyarsky mastodon.social/@jeanneboyarsky IDE Support 55 String result = switch (store)

    { case "Hallmark" -> "KC"; case "Crayola" -> "PA"; default -> "anywhere"; }; return result;
  40. @jeanneboyarsky mastodon.social/@jeanneboyarsky On older Java? public String getLocation(String store) {

    //TODO convert to switch expression on Java 17 String result = ""; switch (store) { case "Hallmark": result = "KC"; break; case "Crayola": result = "PA"; break; default: result = "anywhere"; } return result; } 56
  41. @jeanneboyarsky mastodon.social/@jeanneboyarsky Sequenced Collections 58 21 • addFirst() • addLast()

    • getFirst() • getLast() • removeFirst() • removeLast() Set • reversed() Map • reversed() • sequencedKeySet() • sequencedValues() • sequencedEntrySet() • putFirst(k, v) • putLast(k, v) from NavigableMap • firstEntry() • lastEntry() • pollFirstEntry() • pollLastEntry()
  42. @jeanneboyarsky mastodon.social/@jeanneboyarsky toList() 59 16 public List<String> listLonger( Stream<String> stream)

    { return stream.collect(Collectors.toList()); } public List<String> listShorter( Stream<String> stream) { return stream.toList(); }
  43. @jeanneboyarsky mastodon.social/@jeanneboyarsky Teeing Collector 60 12 record Separations(String spaceSeparated, String

    commaSeparated) {} var list = List.of("x", "y", "z"); Separations result = list.stream() .collect(Collectors.teeing( Collectors.joining(" "), Collectors.joining(","), (s, c) -> new Separations(s, c))); System.out.println(result);
  44. @jeanneboyarsky mastodon.social/@jeanneboyarsky Formatting a String String firstName = "Jeanne"; String

    lastName = "Boyarsky"; String str = String.format( "Hi %s %s!", firstName, lastName); System.out.println(str); System.out.println("Hi %s %s!".formatted( firstName, lastName)); Outputs: Hi Jeanne Boyarsky! Hi Jeanne Boyarsky! 12 61
  45. @jeanneboyarsky mastodon.social/@jeanneboyarsky Common Conversions Conversion What it does %s Formattable

    as String %d Decimal integer (no dot) %c Char %f Float (decimal) %n New line Many more out of scope. Examples: • %e - scientific notation • %t - time • %S - converts to all uppercase 62
  46. @jeanneboyarsky mastodon.social/@jeanneboyarsky Conversion Examples Code Output "%d%%".formatted(1.2) exception "%d%%".formatted(1) 1%

    "%s%%".formatted(1) 1% "%s%%".formatted(1.2) 1.2% “%f%%".formatted(1.2) 1.200000f 12 63
  47. @jeanneboyarsky mastodon.social/@jeanneboyarsky Formatting a Number Char What it does -

    Left justified + Always include +/- space Leading space if positive Char What it does 0 Zero padded , Group numbers ( Negative # in parens 64
  48. @jeanneboyarsky mastodon.social/@jeanneboyarsky Flag Examples Code Output "%,d".formatted(1234) 1,234 "%+d".formatted(1234) 1234

    “% d".formatted(1234) 1234 “%,(d”.formatted(-1234) (1,234) “%,f”.formatted( 1.23456789) 1.234568 12 65
  49. @jeanneboyarsky mastodon.social/@jeanneboyarsky Compact Number NumberFormat defaultFormat = NumberFormat.getCompactNumberInstance(); NumberFormat shortFormat

    = NumberFormat .getCompactNumberInstance( Locale.US, NumberFormat.Style.SHORT); NumberFormat longFormat = NumberFormat .getCompactNumberInstance( Locale.US, NumberFormat.Style.LONG); System.out.println(defaultFormat.format(1_000_000)); System.out.println(shortFormat.format(1_000_000)); System.out.println(longFormat.format(1_000_000)); 1M 1M 1 million 12 66
  50. @jeanneboyarsky mastodon.social/@jeanneboyarsky New Files.mismatch() Path kcdc = Path.of("files/kcdc.txt"); Path kc

    = Path.of("files/kc.txt"); System.out.println(Files.mismatch(kcdc, kc)); System.out.println(Files.mismatch(kcdc, kcdc)); 12 11 (index of first character different) -1 (same file contents regardless of whether exists) 67
  51. @jeanneboyarsky mastodon.social/@jeanneboyarsky Virtual Threads var ex = Executors.newThreadPerTaskExecutor() vs var

    ex = Executors.newVirtualThreadPerTaskExecutor() ex.submit(runnOrCall) // or managed Thread.Builder b = Thread.ofPlatform().name("xxx"); vs Thread.Builder b = Thread.ofVirtual().name("xxx"); b.start(runnable) 21 This could be a whole presentation…. 68