Boyarsky \s </speaker> <title> Becoming one of the first Java 21 \ certified programmers \ (and learning new features) </title> </session> """; continue on next line without a line break new escape character keeps trailing whitespace tab 15 12
ch) public int indexOf(int ch, int fromIndex) public int indexOf(int ch, int fromIndex, int endIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex) public int indexOf(String str, int fromIndex, int endIndex) var name = "animals"; System.out.println(name.indexOf('a', 2, 4)); System.out.println(name.indexOf("al", 2, 5)); Outputs -1 4
HashSet<>(); set.add("Donald"); set.add("Mickey"); set.add("Minnie"); System.out.println(set.iterator().next()); Encounter order undefined(but Mickey on my machine) 21
this text block? String sql = """ select * from mytable where weather = 'snow'; """; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 21
this text block? String sql = """ select * from mytable where weather = 'snow'; """; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 22 A
this text block? String sql = """select * from mytable \ where weather = 'snow'; """; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 23
this text block? String sql = """select * from mytable \ where weather = 'snow'; """; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 24 D (missing opening line break)
this text block? String sql = """ select * \s from mytable \s where weather = 'snow'; """; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 25
this text block? String sql = """ select * \s from mytable \s where weather = 'snow'; """; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 26 B
this text block? String sql = """select * from mytable;"""; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 27
this text block? String sql = """select * from mytable;"""; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 28 D (one line)
be removed without changing the behavior? String sql = """ select \"name\" from mytable \ where value = '\"\""' """; A. 2 B. 3 C. 4 D. Does not compile 31
be removed without changing the behavior? String sql = """ select \"name\" from mytable \ where value = '\"\""' """; A. 2 B. 3 C. 4 D. Does not compile 32 B (around name and second on value)
numPages) { } New type Automatically get * final record * private final instance variables * public accessors * constructor taking both fields * equals * hashCode * no instance initializers 38
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] 39
int numPages, List<String> chapters) { } Book book = new Book("Breaking and entering", 289, chapters); chapters.add("2"); book.chapters().add("3"); System.out.println(book.chapters()); Prints [1,2,3] because shallow immutability 41
Fall, Spring, Summer, Winter { } final class Fall extends Seasons {} final class Spring extends Seasons {} final class Summer extends Seasons {} final class Winter extends Seasons {} Seasons Fall Spring Summer Winter 43
Afternoon, Evening { boolean early(); } public non-sealed class Morning implements TimeOfDay { public boolean early() { return true; } } public non-sealed class Afternoon implements TimeOfDay { public boolean early() { return false; } } public record Evening(int hour) implements TimeOfDay { public boolean early() { return false; } } Records are implicitly final 45
to be removed for this code to compile? public record BBQ(String type) {} public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.setType("pork")); System.out.println(bbq.getType()); System.out.println(bbq.equals(bbq)); } 47 C. 2 D. None of the above A. 0 B. 1
to be removed for this code to compile? public record BBQ(String type) {} public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.setType("pork")); System.out.println(bbq.getType()); System.out.println(bbq.equals(bbq)); } 48 C. 2 D. None of the above A. 0 B. 1 C (no setters and getter should be type())
public record BBQ(String type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 49 C. Does not compile D. None of the above A. chicken B. CHICKEN
public record BBQ(String type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 50 C. Does not compile D. None of the above A. chicken B. CHICKEN C (compact constructor needs to be public because record is)
record BBQ(String type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 51 C. Does not compile D. None of the above A. chicken B. CHICKEN
record BBQ(String type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 52 C. Does not compile D. None of the above A. chicken B. CHICKEN B
public record BBQ(String type) implements Comparable<BBQ> { public int compareTo(BBQ bbq) { return type.compareTo(bbq.type); } } public static void main(String[] args) { BBQ beef = new BBQ("beef"); BBQ pork = new BBQ("pork"); System.out.println(pork.compareTo(beef)); } 53 C. 0 D. Does not compile A. Negative # B. Positive #
public record BBQ(String type) implements Comparable<BBQ> { public int compareTo(BBQ bbq) { return type.compareTo(bbq.type); } } public static void main(String[] args) { BBQ beef = new BBQ("beef"); BBQ pork = new BBQ("pork"); System.out.println(pork.compareTo(beef)); } 54 C. 0 D. Does not compile A. Negative # B. Positive # B
are in this code? public sealed class Phone { class IPhone extends Phone { } class Android extends Phone { } } 56 C. 2 D. 3 A. 0 B. 1 C (extending classes needed to be sealed or final)
case null -> ""; case Integer i when i >= 21 -> "can gamble"; case Integer i -> "too young to gamble"; case String s when "poker".equals(s) -> "table game"; case String s when "craps".equals(s) -> "table game"; case String s -> "other game"; default -> throw new IllegalArgumentException("unexpected type"); }; guard clause pattern variable null allowed Still Null Pointer if don’t specify case null
null -> ""; case Integer i -> "too young to gamble"; case Integer i when i >= 21 -> "can gamble”; // DOES NOT COMPILE case String s -> "other game"; default -> throw new IllegalArgumentException("unexpected type"); }; Order like catching exceptions!
SPADE; } String symbol(Suit suit) { return switch (suit) { case HEART -> "♥"; case Suit.DIAMOND -> "♦"; case CLUB -> "♣"; case Suit.SPADE -> "♠"; }; } Fails compilation if miss one since no default
boolean check ; switch (age) { case Integer i when i <= 21 : check = true; // DOES NOT COMPILE case Integer i when i <= 40 : check = true; default : check = false; } return check; } Can’t use pattern variable if fall thru
"Hallmark" -> "KC"; case "Legoland" -> { int random = new Random().nextInt(); String city = random % 2 == 0 ? "KC" : "Carlsbad"; yield city; } default -> throw new IllegalArgumentException("Unknown"); }; System.out.println(output); Block yield throws exception so no return value needed 75
} private void yield() { String store = "Legoland"; String output = switch(store) { case "Legoland" -> { yield "Carlsbad"; } default -> "other"; }; System.out.println(output); } No to invoke a method called yield, qualify the yield with a receiver or type name 77
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) 79
(str) { case "heart" -> System.out.println("heart"); } Card card = new Card(Suit.HEART, Rank.NUM_2); switch (card) { case Card(Suit suit, Rank rank) when suit == Suit.HEART -> System.out.println("Heart"); case Card c -> System.out.println("other"); } 21 Must be exhaustive if using “when”
do you need to remove to make this code compile? double odds(Object obj) { return switch (obj) { case String s -> throw new IllegalArgumentException("unknown game"); case String s when "blackjack".equals(s) -> .05; case String s when "baccarat".equals(s) -> .12; case Object o -> throw new IllegalArgumentException("unknown game"); default -> throw new IllegalArgumentException("known game"); }; } A. 0 B. 1 C. 2 D. 3
do you need to remove to make this code compile? double odds(Object obj) { return switch (obj) { case String s -> throw new IllegalArgumentException("unknown game"); case String s when "blackjack".equals(s) -> .05; case String s when "baccarat".equals(s) -> .12; case Object o -> throw new IllegalArgumentException("unknown game"); default -> throw new IllegalArgumentException("known game"); }; } A. 0 B. 1 C. 2 D. 3 C String s & Object o or default
print? Object robot = "694"; if (robot instanceof String s) { System.out.print("x"); } if (robot instanceof Integer s) { System.out.print("y"); } System.out.println(robot); 87 C. y694 D. Does not compile A. x694 B. xy694
print? Object robot = "694"; if (robot instanceof String s) { System.out.print("x"); } if (robot instanceof Integer s) { System.out.print("y"); } System.out.println(robot); 88 C. y694 D. Does not compile A. x694 B. xy694 A
this class? class Sword { int length; public boolean equals(Object o) { if (o instanceof Sword sword) { return length == sword.length; } return false; } // assume hashCode properly implemented } 89 C. equals() does not compile A. equals() is correct B. equals() is incorrect
this class? class Sword { int length; public boolean equals(Object o) { if (o instanceof Sword sword) { return length == sword.length; } return false; } // assume hashCode properly implemented } 90 C. equals() does not compile A. equals() is correct B. equals() is incorrect A
fail to compile? Number n = 4; if (n instanceof Integer x) {} if (n instanceof Integer x && x.intValue() > 1) {} if (n instanceof Integer x || x.intValue() > 1) {} if (n instanceof Integer x || x.toString().isEmpty()) {} 91 C. 2 D. 3 A. 0 B. 1
fail to compile? Number n = 4; if (n instanceof Integer x) {} if (n instanceof Integer x && x.intValue() > 1) {} if (n instanceof Integer x || x.intValue() > 1) {} if (n instanceof Integer x || x.toString().isEmpty()) {} 92 C. 2 D. 3 A. 0 B. 1 C (last 2 could be null)
class Sword { int length = 8; public void printLength(Object x) { if (x instanceof Integer length) { length = 2; } System.out.println(length); } } 93 C. 8 D. Does not compile A. 2 B. 3
class Sword { int length = 8; public void printLength(Object x) { if (x instanceof Integer length) { length = 2; } System.out.println(length); } } 94 C. 8 D. Does not compile A. 2 B. 3 C
compile? (more than one) A. if (card instanceof Card (Suit suit, Rank rank)) { } B. if (card instanceof Card (var suit, var rank)) { } C. if (card instanceof Card c) { } D. if (card instanceof Card c.suit, c.rank) { }
compile? (more than one) A. if (card instanceof Card (Suit suit, Rank rank)) { } B. if (card instanceof Card (var suit, var rank)) { } C. if (card instanceof Card c) { } D. if (card instanceof Card c.suit, c.rank) { } A, B, C
and call REST API Proceed CPU Network Still bored… Still waiting for a reply Platform Thread 1 Platform Thread n Do calculation and call REST API Send to threads
calculation and call REST API Proceed CPU Network Thanks for the work! Me too! Platform Thread 1 Platform Thread n Send to threads Virtual Thread t Do calculation and call REST API Virtual Thread t+1 Do calculation and call REST API Virtual Thread x Do calculation and call REST API
to create/with a virtual thread? (more than 1 is correct) A. Executors.newVirtualThread() B. Executors.newVirtualThreadExecutor() C. Executors.newVirtualThreadPerTaskExecutor() D. new VirtualThread() E. Thread.ofVirtual() F. Thread.ofVirtualThread()
to create/with a virtual thread? (more than 1 is correct) A. Executors.newVirtualThread() B. Executors.newVirtualThreadExecutor() C. Executors.newVirtualThreadPerTaskExecutor() D. new VirtualThread() E. Thread.ofVirtual() F. Thread.ofVirtualThread() C, E
to create/with a platform thread? (more than 1 is correct) A. Executors.newCachedThreadPool() B. Executors.newPlatformThreadPool() C. Executors.newPlatformThreadPerTaskExecutor() D. new Thread() E. new PlatformThread() F. Thread.ofPlatform()
to create/with a platform thread? (more than 1 is correct) A. Executors.newCachedThreadPool() B. Executors.newPlatformThreadPool() C. Executors.newPlatformThreadPerTaskExecutor() D. new Thread() E. new PlatformThread() F. Thread.ofPlatform() A, D, F
doStuff() at line v1? try (var service = Executors.newVirtualThreadPerTaskExecutor()) { service.submit(() -> doStuff()); } // line v1 A. doStuff() may be running B. doStuff() was killed C. doStuff() completed D. None of the above 21
doStuff() at line v1? try (var service = Executors.newVirtualThreadPerTaskExecutor()) { service.submit(() -> doStuff()); } // line v1 A. doStuff() may be running B. doStuff() was killed C. doStuff() completed D. None of the above 21 C
UTF-8 is the default character set when reading a file • StringBuilder/StringBuffer have a repeat method • String.splitWithDelimitters(regex, limit) • Finalization deprecated for removal