main(String[] args) {} void main(String... args) {} static void main() {} public void main() {} public void main(String[] args) {} + more If two, calls one with args @jeanneboyarsky 9
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 17
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 18
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 19
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 20
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 21
class Blue { { IO.print("i"); } private Blue() { IO.print("c"); this(1); } D Blue(int num) { IO.print(num); super(); } void main() { new Blue(); } } A. cli B. clicli C. Does not compile D. Does not run @jeanneboyarsky 22
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 23
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 24
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 29
1 21 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 30
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 32
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 38
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 39
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 40
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 41
at 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 42
at 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 43
{ } Automatically get * final record * private final instance variables * public accessors New type * constructor taking both fields * equals * hashCode * no instance initializers * toString @jeanneboyarsky 50
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 @jeanneboyarsky Winter 52
(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 62
""; 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 63
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 64
; 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 @jeanneboyarsky 66
{ 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 70
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 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"); 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 73
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 C. .05 B. NullPointerException D. Does not compile https://linktr.ee/jeanneboyarsky 74
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, B A. IllegalArgumentException C. .05 B. NullPointerException D. Does not compile https://linktr.ee/jeanneboyarsky 75
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); A. x694 B. xy694 C. y694 D. Does not compile @jeanneboyarsky 76
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); A. x694 B. xy694 A C. y694 D. Does not compile @jeanneboyarsky 77
scope? Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 A. 1 B. 1 and 3 C. 1, 2 and 3 D. Does not compile @jeanneboyarsky 78
scope? Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { (int needs to be // line 2 Integer) } D // line 3 A. 1 B. 1 and 3 C. 1, 2 and 3 D. Does not compile @jeanneboyarsky 79
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 } A. equals() is correct B. equals() is incorrect C. equals() does not compile @jeanneboyarsky 80
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 A } A. equals() is correct B. equals() is incorrect C. equals() does not compile @jeanneboyarsky 81
set.add("Donald"); set.add("Mickey"); set.add("Minnie"); System.out.println(set.iterator().next()); Encounter order undefined(but Mickey on my machine) https://linktr.ee/jeanneboyarsky 85
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 C. Three B. Two D. Four https://linktr.ee/jeanneboyarsky 89
lines 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 90
triple slashes use **markdown** /// /// commonmark.org format /// - on odd days, … /// - on even days, … /// /// @return an important number @jeanneboyarsky 92