Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2022-DevNexus-Java12-17.pdf

 2022-DevNexus-Java12-17.pdf

Jeanne Boyarsky

April 13, 2022
Tweet

More Decks by Jeanne Boyarsky

Other Decks in Technology

Transcript

  1. @JeanneBoyarsky @ScottSelikoff Wednesday, April 13, 2022 DevNexus speakerdeck.com/boyarsky Preparing for

    the Java cert and learning new features (part 2 - Java 12 to 17) Jeanne Boyarsky & Scott Selikoff
  2. @JeanneBoyarsky @ScottSelikoff About Us 2 • Java Developer • CodeRanch

    Mod • JUG Leader • Java Champion • Java Developer • CodeRanch Mod • JUG Leader • Software Engineer
  3. @JeanneBoyarsky @ScottSelikoff Jeanne & Scott’s Java 8/11 Cert Books 3

    • OCA: Java 8 Programmer I Study Guide • OCP: Java 8 Programmer II Study Guide • OCA / OCP Java 8 Practice Tests • OCP Java 11 Programmer I Study Guide • OCP Java 11 Programmer II Study Guide • OCP Java 11 Developer Complete Study Guide • OCP Java 11 Practice Tests Win a book at the end!
  4. @JeanneBoyarsky @ScottSelikoff 5 808 809 815 + 816 819 829

    Associate Professional Java 8 Java 11 Java 17 Available exams Also 1Z0-811 (Foundations)
  5. @JeanneBoyarsky @ScottSelikoff Agenda 6 • Text blocks • Switch Expressions

    & Pattern Matching • Records & Sealed classes • Also on the exam – other new features like compact number formatting and helpful null pointers
  6. @JeanneBoyarsky @ScottSelikoff Compare String old = "devnexus,Atlanta ,\"session,workshop\"\n" + “meetup,Various,lecture\n";

    String textBlock = """ devnexus,Atlanta,"session,workshop" meetup,Various,lecture """; 9 Easier to read and code!
  7. @JeanneBoyarsky @ScottSelikoff Essential Whitespace String textBlock = """ <session> <speaker>

    Jeanne Boyarsky </speaker> </session> """; 1 1 incidental whitespace 11 essential whitespace
  8. @JeanneBoyarsky @ScottSelikoff Ending lines String textBlock = """ <session> <speaker>

    Jeanne 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 12
  9. @JeanneBoyarsky @ScottSelikoff New lines String textBlock = """ <session>\n <speaker>

    Jeanne\nBoyarsky </speaker> </session>"""; no line break at end Two new lines (explicit and implicit) One new line (explicit) 13
  10. @JeanneBoyarsky @ScottSelikoff Indent String option1 = " a\n b\n c\n";

    String option2 = "a\nb\nc\n".indent(3); String option3 = """ a b c """.indent(3); Which do you like best? Also normalizes (bye \r) 15
  11. @JeanneBoyarsky @ScottSelikoff Strip Indent Method From beginning From end Per

    line strip() Leading Trailing No stripIndent() Incidental Incidental Yes stripLeading() Leading n/a No stripTrailing() n/a Trailing No 16
  12. @JeanneBoyarsky @ScottSelikoff Question 1 How many lines does this print

    out? String sql = """ select * \n from mytable; """; System.out.print(sql); A. 2 C. 4 B. 3 D. Does not compile 17
  13. @JeanneBoyarsky @ScottSelikoff Question 1 How many lines does this print

    out? String sql = """ select * \n from mytable; """; System.out.print(sql); A. 2 C. 4 B. 3 D. Does not compile 18 C
  14. @JeanneBoyarsky @ScottSelikoff Question 2 How many lines does this print

    out? String sql = """ select * \ from mytable;""".stripIndent(); System.out.print(sql); A. 1 C. 3 B. 2 D. Does not compile 19
  15. @JeanneBoyarsky @ScottSelikoff Question 2 How many lines does this print

    out? String sql = """ select * \ from mytable;""".stripIndent(); System.out.print(sql); A. 1 C. 3 B. 2 D. Does not compile 20 A
  16. @JeanneBoyarsky @ScottSelikoff Question 3 How many lines does this print

    out? String sql = """ select * \s from mytable; ".stripIndent(); System.out.print(sql); A. 1 C. 3 B. 2 D. Does not compile 21
  17. @JeanneBoyarsky @ScottSelikoff Question 3 How many lines does this print

    out? String sql = """ select * \s from mytable; ".stripIndent(); System.out.print(sql); A. 1 C. 3 B. 2 D. Does not compile 22 D
  18. @JeanneBoyarsky @ScottSelikoff Question 4 How many whitespace characters are removed

    by strip()? String sql = """ select "name" from mytable; """; A. 1 C. 3 B. 2 D. Does not compile 23
  19. @JeanneBoyarsky @ScottSelikoff Question 4 How many whitespace characters are removed

    by strip()? String sql = """ select "name" from mytable; """; A. 1 C. 3 B. 2 D. Does not compile 24 C
  20. @JeanneBoyarsky @ScottSelikoff Question 5 How many escapes can be removed

    without changing the behavior? String sql = """ select \"name\" from mytable \ where value = '\"\""' """; A. 2 C. 4 B. 3 D. Does not compile 25
  21. @JeanneBoyarsky @ScottSelikoff Question 5 How many escapes can be removed

    without changing the behavior? String sql = """ select \"name\" from mytable \ where value = '\"\""' """; A. 2 C. 4 B. 3 D. Does not compile 26 B
  22. @JeanneBoyarsky @ScottSelikoff What does this print? String store = "Ponce

    City"; switch(store) { case "Ponce City" : System.out.println("GA"); case "Crayola" : System.out.println("PA"); default: System.out.println("anywhere"); } Three lines (missing break) 28
  23. @JeanneBoyarsky @ScottSelikoff Switch Expressions String store = "Ponce City"; switch(store)

    { case "Ponce City" -> System.out.println("GA"); case "Crayola", "H&R" -> System.out.println("PA"); default -> System.out.println("anywhere"); } Arrow labels No break keyword Multiple values 29
  24. @JeanneBoyarsky @ScottSelikoff Switch Expressions String store = "Ponce City"; String

    output = switch(store) { case "Ponce City" -> "GA"; case "Crayola" -> "PA"; default -> "anywhere"; }; System.out.println(output); Switch returns values 30
  25. @JeanneBoyarsky @ScottSelikoff More features String output = switch(store) { case

    "Ponce City" -> "GA"; case "Legoland" -> { int random = new Random().nextInt(); String city = random % 2 == 0 ? "GA" : "Carlsbad"; yield city; } default -> throw new IllegalArgumentException("Unknown"); }; System.out.println(output); Block yield throws exception so no return value needed 31
  26. @JeanneBoyarsky @ScottSelikoff Is this legal? String store = "Legoland"; String

    output = switch(store) { case "NYC" -> 0; case "Legoland" -> { yield "Carlsbad"; } default -> "PA"; }; System.out.println(output); No, return types must be compatible 32
  27. @JeanneBoyarsky @ScottSelikoff Yield with Switch Stmt enum Position { TOP,

    BOTTOM }; Position pos = Position.TOP; int stmt = switch(pos) { case TOP: yield 1; case BOTTOM: yield 0; }; int expr = switch(pos) { case TOP -> 1; case BOTTOM -> 0; }; Same! 33
  28. @JeanneBoyarsky @ScottSelikoff Missing value enum Position { TOP, BOTTOM };

    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 and not all values covered 34
  29. @JeanneBoyarsky @ScottSelikoff Missing values int greeting = 10; String output

    = switch(greeting) { case 1 -> "Welcome!"; case 10 -> "Goodbye"; }; System.out.println(output); Does not compile. Requires default branch 35
  30. @JeanneBoyarsky @ScottSelikoff Pattern matching for if if (num instanceof Integer)

    { Integer numAsInt = (Integer) num; System.out.println(numAsInt); } if (num instanceof Double) { Double numAsDouble = (Double) num; System.out.println(numAsDouble.intValue()); } if (num instanceof Integer numAsInt) { System.out.println(numAsInt); } if (num instanceof Double numAsDouble) { System.out.println(numAsDouble.intValue()); } Pattern variable 36
  31. @JeanneBoyarsky @ScottSelikoff Flow Scope if (num instanceof Double d1 &&

    d1.intValue() % 2 == 0) { System.out.println(d1.intValue()); } if (num instanceof Double d2 || d2.intValue() % 2 == 0) { System.out.println(d2.intValue()); } Does not compile because d2 might not be double Compiles 37
  32. @JeanneBoyarsky @ScottSelikoff Does this compile? if (num instanceof Double n)

    System.out.println(n.intValue()); if (num instanceof Integer n) System.out.println(n); Yes. Only in scope for if statement 38
  33. @JeanneBoyarsky @ScottSelikoff Does this compile? if (num instanceof Double n)

    System.out.println(n.intValue()); System.out.println(n.intValue()); No. If statement is over 39
  34. @JeanneBoyarsky @ScottSelikoff Does this compile? if (!(num instanceof Double n))

    { return; } System.out.println(n.intValue()); Yes. Returns early so rest is like an else 40
  35. @JeanneBoyarsky @ScottSelikoff Does this compile? if (!(num instanceof Double n))

    { return; } System.out.println(n.intValue()); if (num instanceof Double n) System.out.println(n.intValue()); No. n is still in scope 41
  36. @JeanneBoyarsky @ScottSelikoff Reusing a variable if (num instanceof Integer numAsInt)

    { numAsInt = 6; System.out.println(numAsInt); } Legal. please don’t. 42
  37. @JeanneBoyarsky @ScottSelikoff Question 6 What is output? char ch =

    'b'; int count = 0; switch (ch) { case 'a' -> count++; case 'b' -> count+=2; case 'c' -> count+=3; } System.out.println(count); 43 C. 5 D. Does not compile A. 1 B. 2
  38. @JeanneBoyarsky @ScottSelikoff Question 6 What is output? char ch =

    'b'; int count = 0; switch (ch) { case 'a' -> count++; case 'b' -> count+=2; case 'c' -> count+=3; } System.out.println(count); 44 C. 5 D. Does not compile A. 1 B. 2 B
  39. @JeanneBoyarsky @ScottSelikoff Question 7 What can fill in the blank

    to have the code print 2? char ch = 'b'; ______ value = switch (ch) { case 'a' -> 1; case 'b' -> 2L; case 'c' -> 3.0; default -> 4; }; System.out.println(value); 45 C. Either A or B D. None of the above A. int B. Object
  40. @JeanneBoyarsky @ScottSelikoff Question 7 What can fill in the blank

    to have the code print 2? char ch = 'b'; ______ value = switch (ch) { case 'a' -> 1; case 'b' -> 2L; case 'c' -> 3.0; default -> 4; }; System.out.println(value); 46 C. Either A or B D. None of the above A. int B. Object B
  41. @JeanneBoyarsky @ScottSelikoff Question 8 What does the following 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); 47 C. y694 D. Does not compile A. x694 B. xy694
  42. @JeanneBoyarsky @ScottSelikoff Question 8 What does the following 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); 48 C. y694 D. Does not compile A. x694 B. xy694 A
  43. @JeanneBoyarsky @ScottSelikoff Question 9 Which lines have s in scope?

    Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 49 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3
  44. @JeanneBoyarsky @ScottSelikoff Question 9 Which lines have s in scope?

    Object robot = "694"; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 50 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3 D
  45. @JeanneBoyarsky @ScottSelikoff Question 10 What is true about 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 51 C. equals() does not compile A. equals() is correct B. equals() is incorrect
  46. @JeanneBoyarsky @ScottSelikoff Question 10 What is true about 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 52 C. equals() does not compile A. equals() is correct B. equals() is incorrect A
  47. @JeanneBoyarsky @ScottSelikoff Immutable class 1.Make fields final and private 2.Don’t

    provide setters 3.No subclasses (ex: make class final) 4.Write constructor taking all fields 54
  48. @JeanneBoyarsky @ScottSelikoff Simple Record public record Book (String title, int

    numPages) { } New type Automatically get * final record * private final instance variables * public accessors/getters * constructor taking both fields * equals(), hashCode() and toString() 56
  49. @JeanneBoyarsky @ScottSelikoff Using the Record Book book = new Book("Breaking

    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] 57
  50. @JeanneBoyarsky @ScottSelikoff Add/change methods public record Book (String title, int

    numPages) { @Override public String title() { return '"' + title + '"'; } public boolean isLong() { return numPages > 300; } } Custom method Change behavior 58
  51. @JeanneBoyarsky @ScottSelikoff Immutability public record Book (String title, 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 59
  52. @JeanneBoyarsky @ScottSelikoff Now immutable public record Book (String title, int

    numPages, List<String> chapters) { public Book { chapters = List.copyOf(chapters); } } Must match record access modifier Compact constructor 60 Make immutable copy
  53. @JeanneBoyarsky @ScottSelikoff Sealed classes public abstract sealed class Seasons permits

    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 61
  54. @JeanneBoyarsky @ScottSelikoff Sealed interface public sealed interface TimeOfDay permits Morning,

    Hour, Evening { boolean early(); } public non-sealed class Morning implements TimeOfDay { public boolean early() { return true; } } public non-sealed interface Hour extends TimeOfDay {} public record Evening(int hour) implements TimeOfDay { public boolean early() { return false; } } Records are implicitly final 63
  55. @JeanneBoyarsky @ScottSelikoff Does this compile? public abstract sealed class Seasons

    { } final class Fall extends Seasons {} final class Spring extends Seasons {} final class Summer extends Seasons {} final class Winter extends Seasons {} 64 Yes, but only when they are in the same file.
  56. @JeanneBoyarsky @ScottSelikoff Question 11 How many lines need 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)); } 65 C. 2 D. None of the above A. 0 B. 1
  57. @JeanneBoyarsky @ScottSelikoff Question 11 How many lines need 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)); } 66 C. 2 D. None of the above A. 0 B. 1 C
  58. @JeanneBoyarsky @ScottSelikoff Question 12 What does this output? 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()); } 67 C. Does not compile D. None of the above A. chicken B. CHICKEN
  59. @JeanneBoyarsky @ScottSelikoff Question 12 What does this output? 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()); } 68 C. Does not compile D. None of the above A. chicken B. CHICKEN C
  60. @JeanneBoyarsky @ScottSelikoff Question 13 How many compiler errors 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 ""; } } 69 C. 3 D. 4 A. 1 B. 2
  61. @JeanneBoyarsky @ScottSelikoff Question 13 How many compiler errors 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 ""; } } 70 C. 3 D. 4 A. 1 B. 2 C
  62. @JeanneBoyarsky @ScottSelikoff Question 14 What does this output? 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)); } 71 C. 0 D. Does not compile A. Negative # B. Positive #
  63. @JeanneBoyarsky @ScottSelikoff Question 14 What does this output? 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)); } 72 C. 0 D. Does not compile A. Negative # B. Positive # B
  64. @JeanneBoyarsky @ScottSelikoff Question 15 How many compiler errors are in

    this code? public sealed class Phone { class IPhone extends Phone { } class Android extends Phone { } } 73 C. 2 D. 3 A. 0 B. 1
  65. @JeanneBoyarsky @ScottSelikoff Question 15 How many compiler errors are in

    this code? public sealed class Phone { class IPhone extends Phone { } class Android extends Phone { } } 74 C. 2 D. 3 A. 0 B. 1 C
  66. @JeanneBoyarsky @ScottSelikoff Question 16 Which is true about this text

    block? String sql = """select * from mytable \ where weather = 'snow'; """; A. Has incidental whitespace C. Both A and B B. Has essential whitespace D. Doesn’t compile 76
  67. @JeanneBoyarsky @ScottSelikoff Question 16 Which is true about this text

    block? String sql = """select * from mytable \ where weather = 'snow'; """; A. Has incidental whitespace C. Both A and B B. Has essential whitespace D. Doesn’t compile 77 D
  68. @JeanneBoyarsky @ScottSelikoff Question 17 How many changes are needed to

    have this code print 2? char ch = 'b'; int value = switch (ch) { case 'a' -> 1; case 'b' -> yield 2; case 'c' -> 3; } System.out.println(value); 78 C. 3 D. 4 A. 1 B. 2
  69. @JeanneBoyarsky @ScottSelikoff Question 17 How many changes are needed to

    have this code print 2? char ch = 'b'; int value = switch (ch) { case 'a' -> 1; case 'b' -> yield 2; case 'c' -> 3; } System.out.println(value); 79 C. 3 D. 4 A. 1 B. 2 C
  70. @JeanneBoyarsky @ScottSelikoff Question 18 What does printLength(3) print? class Sword

    { int length = 8; public void printLength(Object x) { if (x instanceof Integer length) length = 2; System.out.println(length); } } 80 C. 8 D. Does not compile A. 2 B. 3
  71. @JeanneBoyarsky @ScottSelikoff Question 18 What does printLength(3) print? class Sword

    { int length = 8; public void printLength(Object x) { if (x instanceof Integer length) length = 2; System.out.println(length); } } 81 C. 8 D. Does not compile A. 2 B. 3 C
  72. @JeanneBoyarsky @ScottSelikoff Question 19 What does this output? record BBQ(String

    type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 82 C. Does not compile D. None of the above A. chicken B. CHICKEN
  73. @JeanneBoyarsky @ScottSelikoff Question 19 What does this output? record BBQ(String

    type) { BBQ { type = type.toUpperCase(); } } public static void main(String[] args) { BBQ bbq = new BBQ("chicken"); System.out.println(bbq.type()); } 83 C. Does not compile D. None of the above A. chicken B. CHICKEN B