still not authorized to tell you any non public information of if I know any :) Some of the material in this presentation appears in our certification books.
Boyarsky \s </speaker> <title> Becoming one of the first Java 17 \ certified programmers \ (and learning new features) continue on next line without a line break new escape character keeps trailing whitespace tab 15 17
option2 = "a\nb\nc\n".indent(3); String option3 = """ a b c """.indent(3); String option4 = """ a Which do you like best? Also normalizes (bye \r) 12 20
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 29
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 30 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 31
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 32 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 33
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 34 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 35
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 36 D (one line)
this print out? String sql = """ select * \s from mytable; ".stripIndent(); System.out.print(sql); A. 1 B. 2 C. 3 D. Does not compile 42 D (missing 2 closing quotes)
are removed by strip()? String sql = """ select "name" from mytable; """; A. 1 B. 2 C. 3 D. Does not compile 44 C (2 leading whitespace + trailing new 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 45
be removed without changing the behavior? String sql = """ select \"name\" from mytable \ where value = '\"\""' """; A. 2 B. 3 C. 4 D. Does not compile 46 B (around name and second on value)
these lines compile? SequencedCollection<Integer> seq1 = new HashSet<>(); SequencedSet<Integer> seq2 = new HashSet<>(); SequencedMap<Integer> seq3 = new HashSet<>(); SequencedCollection<Integer> seq4 = new TreeSet<>(); SequencedSet<Integer> seq5 = new TreeSet<>(); SequencedMap<Integer> seq6 = new TreeSet<>(); A. One B. Two C. Three D. Four 21
these lines compile? SequencedCollection<Integer> seq1 = new HashSet<>(); SequencedSet<Integer> seq2 = new HashSet<>(); SequencedMap<Integer> seq3 = new HashSet<>(); SequencedCollection<Integer> seq4 = new TreeSet<>(); SequencedSet<Integer> seq5 = new TreeSet<>(); SequencedMap<Integer> seq6 = new TreeSet<>(); A. One B. Two C. Three D. Four 21 B (seq4 and seq 5)
numPages) { } New type Automatically get * final record * private final instance variables * public accessors * constructor taking both fields * equals * hashCode * no instance initializers * toString 57
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] 58
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 60
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 62
clause same file Package name optional Permits clause optional same package in named/ unnamed module Package name optional different package in named module Package name required different package in unnamed module Not allowed
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 65
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)); } 68 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)); } 69 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()); } 70 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()); } 71 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()); } 72 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()); } 73 C. Does not compile D. None of the above A. chicken B. CHICKEN B
are in the following code? public final record BBQ(String type) { { type = ""; } public BBQ(String type) { type = type.toUpperCase(); } public void type() { return ""; } public String toString() { return ""; } } 74 C. 3 D. 4 A. 1 B. 2
are in the following code? public final record BBQ(String type) { { type = ""; } public BBQ(String type) { type = type.toUpperCase(); } public void type() { return ""; } public String toString() { return ""; } } 75 C. 3 D. 4 A. 1 B. 2 C (can’t have instance initializer, compact constructor syntax, type() return type)
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)); } 76 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)); } 77 C. 0 D. Does not compile A. Negative # B. Positive # B
static sealed interface Weather permits Wet, Dry{ boolean needUmbrella(); } static non-sealed class Wet implements Weather { public boolean needUmbrella() { return true; } } static record Dry(boolean needUmbrella) implements Weather {} public static void main(String[] args) { Weather weather = new Dry(false); System.out.println(weather.needUmbrella()); } 78 C. Does not compile D. None of the above A. true B. false
static sealed interface Weather permits Wet, Dry{ boolean needUmbrella(); } static non-sealed class Wet implements Weather { public boolean needUmbrella() { return true; } } static record Dry(boolean needUmbrella) implements Weather {} public static void main(String[] args) { Weather weather = new Dry(false); System.out.println(weather.needUmbrella()); } 79 C. Does not compile D. None of the above A. true B. false B
are true? (Choose all that apply) A. There is only one hyphenated modifier in Java (non-sealed) B. A sealed interface may permit another interface. C. Sealed records are allowed D. Sealed enums are allowed 80
are true? (Choose all that apply) A. There is only one hyphenated modifier in Java (non-sealed) B. A sealed interface may permit another interface. C. Sealed records are allowed D. Sealed enums are allowed 81 A, B
could Android be? (Choose all that apply) package general; static sealed class Phone permits IPhone, Android { } A. In the same file as Phone B. In the same package as Phone within a module C. In a different package from Phone, but in the same module D. In a different module 82
could Android be? (Choose all that apply) package general; static sealed class Phone permits IPhone, Android { } A. In the same file as Phone B. In the same package as Phone within a module C. In a different package from Phone, but in the same module D. In a different module 83 A, B
could Android be? (Choose all that apply) package general; static sealed class Phone permits mac.IPhone, google.Android { } A. In the same file as Phone B. In the same package as Phone within a module C. In a different package from Phone, but in the same module D. In a different module 84
could Android be? (Choose all that apply) package general; static sealed class Phone permits mac.IPhone, google.Android { } A. In the same file as Phone B. In the same package as Phone within a module C. In a different package from Phone, but in the same module D. In a different module 85 C, D
are in this code? public sealed class Phone { class IPhone extends Phone { } class Android extends Phone { } } 87 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 109
} 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 111
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) 113
(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
results of odds(“slots”) and odds(null) run separately? double odds(String str) { return switch (str) { case String s when "blackjack".equals(s) -> .05; case String s when "baccarat".equals(s) -> .12; case String s -> throw new IllegalArgumentException("unknown game"); }; } A. IllegalArgumentException B. NullPointerException C. .05 D. Does not compile
results of odds(“slots”) and odds(null) run separately? double odds(String str) { return switch (str) { case String s when "blackjack".equals(s) -> .05; case String s when "baccarat".equals(s) -> .12; case String s -> throw new IllegalArgumentException("unknown game"); }; } A. IllegalArgumentException B. NullPointerException C. .05 D. Does not compile A, B
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); 123 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); 124 C. y694 D. Does not compile A. x694 B. xy694 A
in scope? Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 125 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3
in scope? Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 126 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3 D (int needs to be Integer)
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 } 127 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 } 128 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()) {} 129 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()) {} 130 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); } } 131 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); } } 132 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
output of the following? Card card = new Card(Suit.HEART, Rank.NUM_2); switch (card) { case Card(var suit, var rank) when suit == Suit.HEART && rank == Rank.ACE -> System.out.println("Ace of Hearts"); case Card(var suit, var rank) when suit == Suit.HEART -> System.out.println("Hearts"); } A. Ace of Hearts B. Hearts C. Null Pointer D. Does not compile
output of the following? Card card = new Card(Suit.HEART, Rank.NUM_2); switch (card) { case Card(var suit, var rank) when suit == Suit.HEART && rank == Rank.ACE -> System.out.println("Ace of Hearts"); case Card(var suit, var rank) when suit == Suit.HEART -> System.out.println("Hearts"); } A. Ace of Hearts B. Hearts C. Null Pointer D. Does not compile D (no case Card)
(Suit suit, Rank rank), how many of these compile? case Card(var rank, var suit) when suit == Suit.HEART case Card(Rank rank, Suit suit) when suit == Suit.HEART case Card(Rank rank, var suit) when suit == Suit.HEART case Card(var suit, var rank) when suit == Suit.HEART case Card(Suit suit, Rank rank) when suit == Suit.HEART case Card(var suit, Rank rank) when suit == Suit.HEART A. 0 B. 1 C. 2 D. 3 E. 4 F. 5
(Suit suit, Rank rank), how many of these compile? case Card(var rank, var suit) when suit == Suit.HEART case Card(Rank rank, Suit suit) when suit == Suit.HEART case Card(Rank rank, var suit) when suit == Suit.HEART case Card(var suit, var rank) when suit == Suit.HEART case Card(Suit suit, Rank rank) when suit == Suit.HEART case Card(var suit, Rank rank) when suit == Suit.HEART A. 0 B. 1 C. 2 D. 3 E. 4 F. 5 D (last three)
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 21
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);
lots of code here System.out.println(path.toAbsolutePath()); % java11 HelpfulNullPointer.java Exception in thread "main" java.lang.NullPointerException at HelpfulNullPointer.main(HelpfulNullPointer.java:9) % java17 HelpfulNullPointer.java Exception in thread "main" java.lang.NullPointerException: Cannot invoke “java.nio.file.Path.toAbsolutePath()" because “path" is null at HelpfulNullPointer.main(HelpfulNullPointer.java:9) 14 158
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
List.of(1,2,3).stream() .collect(Collectors.teeing( Collectors.toSet(), System::forEach)); A. 1,2,3 B. {1,2,3} C. Both A and B D. Does not compile E. None of the above 178
List.of(1,2,3).stream() .collect(Collectors.teeing( Collectors.toSet(), System::forEach)); A. 1,2,3 B. {1,2,3} C. Both A and B D. Does not compile E. None of the above 179 D (teeing takes 2 collectors and a combiner)
System.out.println("%s %d".formatted( "KCDC", "2021")); A. KCDC2021 B. KCDC2021.0 C. KCDC 2021 D. KCDC 2021.0 E. None of the above 181 E (exception because 2021 is a string)
are valid locales? Locale a = new Locale("en"); Locale b = new Locale("US"); Locale c = Locale.of("en"); Locale d = Locale.of("US"); A. 0 B. 1 C. 2 D. 3 E. 4 182
are valid locales? Locale a = new Locale("en"); Locale b = new Locale("US"); Locale c = Locale.of("US"); Locale d = Locale.of("US"); A. 0 B. 1 C. 2 D. 3 E. 4 183 C (first and third “US” needs language)
•Java 17 - Unicode 17 public class Fun { public static void main(String[] args) { System.out.println(""" Wear \uD83E\uDD7D \ and \uD83E\uDD7C \ when using a \uD83E\uDDEA"""); } } 189 java17 Fun.java Wear 🥽 and 🥼 when using a 🧪
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