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. https://linktr.ee/jeanneboyarsky 4
exercises •10 minute break This means if a colleague needs to call you, the last 15-20 minutes of each hour is best. https://linktr.ee/jeanneboyarsky 7
void main(String... args) {} static void main() {} public void main() {} public void main(String[] args) {} + more If two, calls one with args @jeanneboyarsky 15
All other System.out methods such as printf, format • All other System.in methods such as read() • Ability to wrap in System.in in Scanner • Setting streams to different values @jeanneboyarsky 18
main methods? A. public static void main(String[] args) {} B. void main(String args) {} C. final void main() { } D. private void main(String[] args) {} @jeanneboyarsky 23
main methods? A. public static void main(String[] args) {} A, C B. void main(String args) {} (B wrong type, D private) C. final void main() { } D. private void main(String[] args) {} @jeanneboyarsky 24
main methods? (yes more) A. main() {} B. public void main() {} C. void main(String... args) { } D. void final main(String[] args) {} @jeanneboyarsky 25
main methods? (yes more) A. main() {} B, C B. public void main() (A no{} void, D final order) C. void main(String... args) { } D. void final main(String[] args) {} @jeanneboyarsky 26
a class named Foo.java? 1: private static void print() { 2: IO.println("Ran!"); 3: } 4: void main() { 5: Foo.print(); 6: } A. Ran! B. Does not compile @jeanneboyarsky 29
a class named Foo.java? 1: private static void print() { 2: IO.println("Ran!"); 3: } 4: void main() { (static) 5: Foo.print(); 6: } B A. Ran! B. Does not compile @jeanneboyarsky 30
when running javac Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac @jeanneboyarsky 37
when running javac Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac C @jeanneboyarsky 38
when running java Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac @jeanneboyarsky 39
when running java Container.java in a directory containing only Container.java and Contains.java? public Container { private Contains c; } A. 0 C. 2 B. 1 D. Error running javac A @jeanneboyarsky 40
5? 1: import java.util.*; 2: import jeanne.List; 3: import module java.base; 4: 5: void main(List list) {} A. The import on line 1 is used for line 5 B. The import on line 2 is used for line 5 C. The import on line 3 is used for line 5 D. This program will run @jeanneboyarsky 41
5? 1: import java.util.*; 2: import jeanne.List; 3: import module java.base; 4: 5: void main(List list) {} B A. The import on line 1 is used for line 5 B. The import on line 2 is used for line 5 C. The import on line 3 is used for line 5 D. This program will run @jeanneboyarsky 42
(obj) { 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"); }; pattern variable Still Null Pointer if don’t specify case null @jeanneboyarsky 51
""; 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! @jeanneboyarsky 52
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 @jeanneboyarsky 53
{ 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"); } Must be exhaustive if using “when” https://linktr.ee/jeanneboyarsky 57
s) { IO.print(s); super(); } private class Blue { { IO.print("i"); } private Blue() { IO.print("c"); this("l"); } void main() { new Blue(); } } A. cli B. clicli C. Does not compile D. Does not run @jeanneboyarsky 59
class Blue { { IO.print("i"); } private Blue() { IO.print("c"); this("l"); } B Blue(String s) { IO.print(s); super(); } void main() { new Blue(); } } A. cli B. clicli C. Does not compile D. Does not run @jeanneboyarsky 60
replaced by IO.println(shade); 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } 9: A. 3} C. 6 B. 5 D. 7 @jeanneboyarsky 61
replaced by IO.println(shade); 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } 9: A. 3} C. 6 C, D B. 5 D. 7 @jeanneboyarsky 62
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 63
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } B, C A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 64
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 65
replaced by shade = "sky blue"; 1: public class Blue { 2: private String shade = "sky blue"; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } C, D A. 5 B. 6 D. line 5 can have the replacement applied twice @jeanneboyarsky 66
replaced by shade = "sky blue"; 1: public class Blue { 2: private final String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } D. line 5 can have A. 5 B. 6 the replacement applied twice @jeanneboyarsky 67
replaced by shade = "sky blue"; 1: public class Blue { 2: private final String shade; 3: 4: Blue() { 5: 6: super(); 7: 8: } C. 7 9: } D. line 5 can have A, B, C A. 5 B. 6 the replacement applied twice @jeanneboyarsky 68
1: void main(String[] _) { 2: double _ = 0.00; 3: String _ = null; 4: IO.println(_); 5: boolean isNull = _ == null; 6: } A. Line 1 B. Line 2 C. Line 3 D. Line 4 E. Line 5 @jeanneboyarsky 69
1: void main(String[] _) { 2: double _ = 0.00; 3: String _ = null; 4: IO.println(_); 5: boolean isNull = _ == null; 6: } A, D, E A. Line 1 B. Line 2 C. Line 3 D. Line 4 E. Line 5 @jeanneboyarsky 70
by _? void main(String[] args) { enum Suit { HEART, DIAMOND, SPADE, CLUB }; enum Symbol { ACE, JACK, QUEENS, KING}; record Card(Suit suit, Symbol symbol) {} var card = new Card(Suit.HEART, Symbol.ACE); try { if (card instanceof Card(Suit suit, Symbol symbol)) IO.println(suit); } catch (NullPointerException e) { IO.print("party!"); } } A. args B. card C. symbol D. e @jeanneboyarsky 71
by _? void main(String[] args) { enum Suit { HEART, DIAMOND, SPADE, CLUB }; enum Symbol { ACE, JACK, QUEENS, KING}; record Card(Suit suit, Symbol symbol) {} var card = new Card(Suit.HEART, Symbol.ACE); try { if (card instanceof Card(Suit suit, Symbol symbol)) IO.println(suit); } catch (NullPointerException e) { IO.print("party!"); } } C, D A. args B. card C. symbol D. e @jeanneboyarsky 72
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 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 73
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"); String s & Object default -> throw new IllegalArgumentException("known game"); o or default }; } C A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 74
replace with _? double odds(Object obj) { return switch (obj) { case String s when "blackjack".equals(s) -> .05; case String s -> throw new IllegalArgumentException("unknown game"); case Object o -> throw new IllegalArgumentException("unknown game"); }; } A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 75
replace with _? double odds(Object obj) { return switch (obj) { case String s when "blackjack".equals(s) -> .05; case String s -> throw new IllegalArgumentException("unknown game"); case Object o -> throw new IllegalArgumentException("unknown game"); }; } C A. 0 C. 2 B. 1 D. 3 https://linktr.ee/jeanneboyarsky 76
be replaced by _? private int unused; void transform(List<String> list) { if (list instanceof ArrayList arr) try { list.removeIf(x -> true); } catch (NullPointerException e) { } } A. unused B. arr C. x D. e https://linktr.ee/jeanneboyarsky 77
be replaced by _? private int unused; void transform(List<String> list) { if (list instanceof ArrayList arr) try { list.removeIf(x -> true); } catch (NullPointerException e) { } } B, C, D A. unused B. arr C. x D. e https://linktr.ee/jeanneboyarsky 78
possibly smaller last one Gatherers.windowSliding() Groups of size x differing by 1 Gatherers.fold() Combine elements into value Gatherers.scan() Accumulates storing intermediate Gatherers.mapConcurrent() Map with parallelism https://linktr.ee/jeanneboyarsky 83
finisher) • Gatherer.ofSequential(initializer, integrator, finisher) No combiner as don’t need to merge intermediate states when sequential @jeanneboyarsky 91
set.add("Donald"); set.add("Mickey"); set.add("Minnie"); System.out.println(set.iterator().next()); Encounter order undefined(but Mickey on my https://linktr.ee/jeanneboyarsky 99
gatherer returns the value for the next step in the pipeline at the end of the gatherer A. Ender C. StreamOperation B. Finisher D. Terminator https://linktr.ee/jeanneboyarsky 109
gatherer returns the value for the next step in the pipeline at the end of the gatherer B A. Ender C. StreamOperation B. Finisher D. Terminator https://linktr.ee/jeanneboyarsky 110
multiple elements to the stream what parts can push to the downstream? A. Initializer C. Combiner B. Integrator D. Finisher https://linktr.ee/jeanneboyarsky 111
multiple elements to the stream what parts can push to the downstream? B, D A. Initializer C. Combiner B. Integrator D. Finisher https://linktr.ee/jeanneboyarsky 112
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowFixed(4)) .toList(); IO.println(list); } A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 117
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowFixed(4)) .toList(); IO.println(list); } B A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 118
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowSliding(4)) .toList(); IO.println(list); } A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 119
the following? var list = Stream.iterate(1, i -> i + 1) .limit(5) .gather(Gatherers.windowSliding(4)) .toList(); IO.println(list); } C A. [[1, 2, 3, 4]] B. [[1, 2, 3, 4], [5]] C. [[1, 2, 3, 4], [2, 3, 4, 5]] D. None of the above https://linktr.ee/jeanneboyarsky 120
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 C. Three B. Two D. Four https://linktr.ee/jeanneboyarsky 121
compile? SequencedCollection<Integer> seq1 = new HashSet<>(); SequencedSet<Integer> seq2 = new HashSet<>(); SequencedMap<Integer> seq3 = new HashSet<>(); SequencedCollection<Integer> seq4 = new TreeSet<>(); (seq4=and 5) SequencedSet<Integer> seq5 newseq TreeSet<>(); SequencedMap<Integer> seq6 = new TreeSet<>(); B A. One C. Three B. Two D. Four https://linktr.ee/jeanneboyarsky 122
Thread n Still bored… Send to threads Do calculation and call REST API Do calculation and call REST API Proceed Network https://linktr.ee/jeanneboyarsky CPU Still waiting for a reply 128
1 Platform Thread n Virtual Thread 1 Virtual Thread t+1 Do calculation and call REST API Do calculation and call REST API Virtual Thread t Virtual Thread x Do calculation and call REST API Do calculation and call REST API Thanks for the work! CPU Me too! Network https://linktr.ee/jeanneboyarsky 129
OS threads Runs on OS thread but doesn’t monopolize Often shortlived Often pooled Lightweight Instance of java.lang.Thread Instance of java.lang.Thread https://linktr.ee/jeanneboyarsky 131
triple slashes use **markdown** /// /// commonmark.org format /// - on odd days, … /// - on even days, … /// /// @return an important number @jeanneboyarsky 139
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() https://linktr.ee/jeanneboyarsky 140
a virtual thread? (more than 1 is correct) A. Executors.newVirtualThread() B. Executors.newVirtualThreadExecutor() C, E C. Executors.newVirtualThreadPerTaskExecutor() D. new VirtualThread() E. Thread.ofVirtual() F. Thread.ofVirtualThread() https://linktr.ee/jeanneboyarsky 141
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() https://linktr.ee/jeanneboyarsky 142
a platform thread? (more than 1 is correct) A. Executors.newCachedThreadPool() B. Executors.newPlatformThreadPool() A, D, F C. Executors.newPlatformThreadPerTaskExecutor() D. new Thread() E. new PlatformThread() F. Thread.ofPlatform() https://linktr.ee/jeanneboyarsky 143
line v1? try (var service = Executors.newVirtualThreadPerTaskExecutor()) { service.submit(() -> doStuff()); } // line v1 A. doStuff() may be running C. doStuff() completed B. doStuff() was killed D. None of the above https://linktr.ee/jeanneboyarsky 144
line v1? try (var service = Executors.newVirtualThreadPerTaskExecutor()) { service.submit(() -> doStuff()); } C // line v1 A. doStuff() may be running C. doStuff() completed B. doStuff() was killed D. None of the above https://linktr.ee/jeanneboyarsky 145
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 148
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } D A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 149
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { ScopedValue.where(ID, 10) .run(this::timing); } A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 150
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.get()); } void main() { ScopedValue.where(ID, 10) .run(this::timing); } B A. 0 B. 10 C. Code does not compile D. Throws an exception @jeanneboyarsky 151
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.orElse(2)); } void main() { timing(); } A. 0 B. 2 C. Code does not compile D. Throws an exception @jeanneboyarsky 152
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); B void timing() { IO.print(LENGTH.orElse(2)); } void main() { timing(); } A. 0 B. 2 C. Code does not compile D. Throws an exception @jeanneboyarsky 153
the following? static final ScopedValue<Integer> LENGTH = ScopedValue.newInstance(); void timing() { IO.print(LENGTH.isBound()); } void main() { timing(); ScopedValue.where(ID, 10) .run(this::timing); } B A. falsefalse B. falsetrue C. truefalse D. truetrue @jeanneboyarsky 155