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 17
convert to text block when on Java 17 String json = "{" + " \"query\": \"%s\"" + " \"start\": \"1\"," + " \"end\": \"10\"" + "}"; return String.format(json, search); } 23 Hard to read but positions for future
""; switch (store) { case "Hallmark": result = "KC"; break; case "Crayola": result = "PA"; break; default: result = "anywhere"; } return result; } You remembered the breaks, right? 38
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) 40
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 41
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 42
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; } 45
{ } New type Automatically get * final record * private final instance variables * public accessors * constructor taking both fields * equals * hashCode 49
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] 50
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 53 Be sure to use al fields for equals/ hashCode
{} 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);
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 59