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

2022-DevNexus-Java9-11.pdf

 2022-DevNexus-Java9-11.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 1 - Java 9 to 11)
    Jeanne Boyarsky & Scott Selikoff

    View full-size slide

  2. @JeanneBoyarsky @ScottSelikoff
    About Us
    2
    • Java Developer
    • CodeRanch Mod
    • JUG Leader
    • Java Champion
    • Java Developer
    • CodeRanch Mod
    • JUG Leader
    • Software Engineer

    View full-size slide

  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!

    View full-size slide

  4. @JeanneBoyarsky @ScottSelikoff
    Pre-order Java 17 Cert Books
    4
    May 2022 Sept 2022

    View full-size slide

  5. @JeanneBoyarsky @ScottSelikoff 5
    808
    809 815
    +
    816
    819 829
    Associate
    Professional
    Java 8 Java 11 Java 17
    Available exams
    Also 1Z0-811
    (Foundations)

    View full-size slide

  6. @JeanneBoyarsky @ScottSelikoff
    Agenda
    6
    • var
    • Collections and Streams
    • Modules
    • Also on exam – other new features like:
    • Effectively final in try with resources
    • Changes to interfaces
    • _ no longer being allowed

    View full-size slide

  7. @JeanneBoyarsky @ScottSelikoff
    Type Inference: var
    7

    View full-size slide

  8. @JeanneBoyarsky @ScottSelikoff
    String name = "Jill";
    var name = "Jill";
    List list = List.of(1, 2, 3);
    var list = List.of(1, 2, 3);
    • Syntactical sugar/less boilerplate
    • Type Fixed
    • Value Mutable (unlike val from Kotlin)
    8

    View full-size slide

  9. @JeanneBoyarsky @ScottSelikoff
    var name = "Jill";
    String name2 = name;
    Compiled code same with var
    9

    View full-size slide

  10. @JeanneBoyarsky @ScottSelikoff
    Map> grade1= new
    HashMap>();
    vs
    Map> grade1 = new
    HashMap<>();
    vs
    var grade1 = new HashMapList>();
    10

    View full-size slide

  11. @JeanneBoyarsky @ScottSelikoff
    var csvPath = createAndGetFile(CSV_DATA);
    try (var csvWriter =
    Files.newBufferedWriter(csvPath);
    var csvPrinter = new CSVPrinter(
    csvWriter, CSVFormat.DEFAULT)); {
    }
    11

    View full-size slide

  12. @JeanneBoyarsky @ScottSelikoff
    Where can we use?
    var name = "Jill";
    var other = name + 2;
    var list = List.of(1, 2, 3);
    for (var num : list) {}
    12

    View full-size slide

  13. @JeanneBoyarsky @ScottSelikoff
    Where can’t we use?
    class Inner { var bad = "abc"; }
    class var {}
    var noGood;
    noGood = 4;
    Also instance variables, etc
    13

    View full-size slide

  14. @JeanneBoyarsky @ScottSelikoff
    Does this work?
    var var = "var";
    14

    View full-size slide

  15. @JeanneBoyarsky @ScottSelikoff
    Pros Cons
    Less typing Loss of information
    Less redundancy Variable names matter
    more
    Can scan variable
    names
    Be careful!
    15
    http://openjdk.java.net/projects/amber/LVTIstyle.html

    View full-size slide

  16. @JeanneBoyarsky @ScottSelikoff
    Lambdas
    Predicate pred1 =
    p -> true;
    Predicate pred2 =
    (String p) -> true;
    Predicate pred3 =
    (var p) -> true;
    16

    View full-size slide

  17. @JeanneBoyarsky @ScottSelikoff
    All or nothing
    Good
    (var map, var list) -> true
    No good
    (var map, list) -> true
    (var map, List list) -> true
    17

    View full-size slide

  18. @JeanneBoyarsky @ScottSelikoff
    Question 1
    What is the output?
    var a = 1; var b = a;
    int c = b; System.out.println(c);
    A. a does not compile
    B. b does not compile
    C. c does not compile
    18
    D. 1
    E. None of the above

    View full-size slide

  19. @JeanneBoyarsky @ScottSelikoff
    Question 1
    What is the output?
    var a = 1; var b = a;
    int c = b; System.out.println(c);
    A. a does not compile
    B. b does not compile
    C. c does not compile
    19
    D. 1
    E. None of the above
    D

    View full-size slide

  20. @JeanneBoyarsky @ScottSelikoff
    Question 2
    What is the output?
    var a = 1; var b = a;
    double c = b; System.out.println(c);
    A. a does not compile
    B. b does not compile
    C. c does not compile
    20
    D. 1.0
    E. None of the above

    View full-size slide

  21. @JeanneBoyarsky @ScottSelikoff
    Question 2
    What is the output?
    var a = 1; var b = a;
    double c = b; System.out.println(c);
    A. a does not compile
    B. b does not compile
    C. c does not compile
    21
    D. 1.0
    E. None of the above
    D

    View full-size slide

  22. @JeanneBoyarsky @ScottSelikoff
    Question 3
    What is the output?
    var a = 1; var b = a;
    String c = b; System.out.println(c);
    A. a does not compile
    B. b does not compile
    C. c does not compile
    22
    D. 1
    E. None of the above

    View full-size slide

  23. @JeanneBoyarsky @ScottSelikoff
    Question 3
    What is the output?
    var a = 1; var b = a;
    String c = b; System.out.println(c);
    A. a does not compile
    B. b does not compile
    C. c does not compile
    23
    D. 1
    E. None of the above
    C

    View full-size slide

  24. @JeanneBoyarsky @ScottSelikoff
    Question 4
    Which variable declarations compile?
    var a = null; var b = 1;
    var c; var var = 3;
    A. a
    B. b
    24
    C. c
    D. var

    View full-size slide

  25. @JeanneBoyarsky @ScottSelikoff
    Question 4
    Which variable declarations compile?
    var a = null; var b = 1;
    var c; var var = 3;
    A. a
    B. b
    25
    C. c
    D. var
    B, D

    View full-size slide

  26. @JeanneBoyarsky @ScottSelikoff
    Question 5
    Which variable declarations compile?
    var a; a = 1; static var b = 1;
    int x=1, var y=1; var VAR = 1;
    A. a
    B. b
    26
    C. y
    D. VAR

    View full-size slide

  27. @JeanneBoyarsky @ScottSelikoff
    Question 5
    Which variable declarations compile?
    var a; a = 1; static var b = 1;
    int x=1, var y=1; var VAR = 1;
    A. a
    B. b
    27
    C. y
    D. VAR
    D

    View full-size slide

  28. @JeanneBoyarsky @ScottSelikoff
    Collections and Streams
    28

    View full-size slide

  29. @JeanneBoyarsky @ScottSelikoff
    Creating a non-empty Set
    Set set = new HashSet<>(
    Arrays.asList("1","2", "3"));
    Set set = Stream.of("1", "2”, "3")
    .collect(Collectors.toSet());
    Set set = Set.of("1", "2", "3");

    View full-size slide

  30. @JeanneBoyarsky @ScottSelikoff
    For consistency
    Map map = Map.of("k", "v");
    Up to 10 params, then varargs
    List list = List.of("1", "2", "3");
    Up to 20 params, then varargs

    View full-size slide

  31. @JeanneBoyarsky @ScottSelikoff
    Better Map
    Map map =
    Map.ofEntries(
    Map.entry(1, 10),
    Map.entry(2, 20)
    );

    View full-size slide

  32. @JeanneBoyarsky @ScottSelikoff
    Lists
    • Not equivalent!
    • List.of(1, 2, 3)
    • Arrays.asList(1, 2, 3)
    • Only one is immutable
    32

    View full-size slide

  33. @JeanneBoyarsky @ScottSelikoff
    Streams
    "abc\n123"
    .lines()
    .count();
    "abc".chars()
    .count();
    33

    View full-size slide

  34. @JeanneBoyarsky @ScottSelikoff
    Streams - ofNullable
    stream = dubiousObj == null
    ? Stream.empty()
    : Stream.of(dubiousObj);
    stream = Stream.ofNullable(dubiousObj);
    Optional.ofNullable()
    existed since Java 8

    View full-size slide

  35. @JeanneBoyarsky @ScottSelikoff
    Streams - iterate
    Stream.iterate(10, i-> i-1)
    .limit(10)
    .forEach(System.out::println);
    Stream.iterate(10, i-> i>0, i-> i-1)
    .forEach(System.out::println);

    View full-size slide

  36. @JeanneBoyarsky @ScottSelikoff
    Streams - takeWhile
    Stream.iterate(1, i-> i< 10, i-> i + 1)
    .takeWhile(i -> i < 5)
    .forEach(System.out::println);
    Assumes ordered stream. Takes all elements until one
    doesn’t match. So prints the numbers 1-4

    View full-size slide

  37. @JeanneBoyarsky @ScottSelikoff
    Streams - takeWhile
    Stream.iterate(
    new SimpleEntry(1,1),
    e -> new SimpleEntry(
    e.getValue(), e.getKey()+e.getValue()))
    .map(Entry::getValue)
    .takeWhile(n -> n < 30)
    .forEach(System.out::println);
    How print all Fibonacci #s less than 30?

    View full-size slide

  38. @JeanneBoyarsky @ScottSelikoff
    Streams - dropWhile
    Stream.iterate(1, i-> i< 10, i-> i + 1)
    .dropWhile(i -> i < 5)
    .forEach(System.out::println);
    Assumes ordered stream. Takes all elements until one
    doesn’t match. So prints the numbers 5-9

    View full-size slide

  39. @JeanneBoyarsky @ScottSelikoff
    Streams - dropWhile
    Stream.iterate(new SimpleEntry(1,1),
    e -> new SimpleEntry(
    e.getValue(), e.getKey()+e.getValue()))
    .map(Entry::getValue)
    .dropWhile(n -> n < 30)
    .forEach(System.out::println);
    Note: This doesn’t work. takeWhile and dropWhile aren’t
    always opposites.

    View full-size slide

  40. @JeanneBoyarsky @ScottSelikoff
    Question 6
    What does the following output?
    List list = List.of(1, 2, 3);
    list.remove(2);
    System.out.println(list);
    A. [1, 2, 3]
    B. [1, 3]
    C. [1, 2]
    40
    D. Code does not
    compile
    E. Throws exception

    View full-size slide

  41. @JeanneBoyarsky @ScottSelikoff
    Question 6
    What does the following output?
    List list = List.of(1, 2, 3);
    list.remove(2);
    System.out.println(list);
    A. [1, 2, 3]
    B. [1, 3]
    C. [1, 2]
    41
    D. Code does not
    compile
    E. Throws exception
    E

    View full-size slide

  42. @JeanneBoyarsky @ScottSelikoff
    Question 7
    What does the following output?
    List list = Arrays.asList(1, 2, 3);
    list.remove(2);
    System.out.println(list);
    A. [1, 2, 3]
    B. [1, 3]
    C. [1, 2]
    42
    D. Code does not
    compile
    E. Throws exception

    View full-size slide

  43. @JeanneBoyarsky @ScottSelikoff
    Question 7
    What does the following output?
    List list = Arrays.asList(1, 2, 3);
    list.remove(2);
    System.out.println(list);
    A. [1, 2, 3]
    B. [1, 3]
    C. [1, 2]
    43
    D. Code does not
    compile
    E. Throws exception
    E

    View full-size slide

  44. @JeanneBoyarsky @ScottSelikoff
    Question 8
    What does the following output?
    long count = Stream
    .iterate(1; i-> i< 10; i-> i+2).count();
    System.out.println(count);
    A. 0
    B. 5
    C. 10
    44
    D. The code does not
    compile
    E. None of the above

    View full-size slide

  45. @JeanneBoyarsky @ScottSelikoff
    Question 8
    What does the following output?
    long count = Stream
    .iterate(1; i-> i< 10; i-> i+2).count();
    System.out.println(count);
    A. 0
    B. 5
    C. 10
    45
    D. The code does not
    compile
    E. None of the above
    D

    View full-size slide

  46. @JeanneBoyarsky @ScottSelikoff
    Question 9
    What does the following output?
    Stream.iterate(1, i-> i< 10, i-> i++)
    .takeWhile(i -> i < 5)
    .forEach(System.out::println);
    A. The numbers 1-4
    B. The numbers 5-9
    C. An infinite stream
    46
    D. The code does not
    compile
    E. None of the above

    View full-size slide

  47. @JeanneBoyarsky @ScottSelikoff
    Question 9
    What does the following output?
    Stream.iterate(1, i-> i< 10, i-> i++)
    .takeWhile(i -> i < 5)
    .forEach(System.out::println);
    A. The numbers 1-4
    B. The numbers 5-9
    C. An infinite stream
    47
    D. The code does not
    compile
    E. None of the above
    C

    View full-size slide

  48. @JeanneBoyarsky @ScottSelikoff
    Question 10
    What does the following output?
    long count = Stream
    .iterate(1, i-> i< 10, i-> i+2).count();
    System.out.println(count);
    A. 0
    B. 5
    C. 10
    48
    D. The code does not
    compile
    E. None of the above

    View full-size slide

  49. @JeanneBoyarsky @ScottSelikoff
    Question 10
    What does the following output?
    long count = Stream
    .iterate(1, i-> i< 10, i-> i+2).count();
    System.out.println(count);
    A. 0
    B. 5
    C. 10
    49
    D. The code does not
    compile
    E. None of the above
    B

    View full-size slide

  50. @JeanneBoyarsky @ScottSelikoff
    Modules
    50

    View full-size slide

  51. @JeanneBoyarsky @ScottSelikoff
    Module basics
    • Module system
    • JDK is modularized
    51

    View full-size slide

  52. @JeanneBoyarsky @ScottSelikoff
    Simple module
    /src
    /package1
    /module-info.class
    /MyClass.class
    52

    View full-size slide

  53. @JeanneBoyarsky @ScottSelikoff
    Module info
    module moduleName {
    requires otherModuleName;
    exports packageName;
    }
    53

    View full-size slide

  54. @JeanneBoyarsky @ScottSelikoff
    Base Module
    54

    View full-size slide

  55. @JeanneBoyarsky @ScottSelikoff
    Module graph
    55

    View full-size slide

  56. @JeanneBoyarsky @ScottSelikoff
    Module file “commands”
    • requires – dependencies
    • requires transitive – grant to caller
    • exports – share package
    • opens – for reflection
    56

    View full-size slide

  57. @JeanneBoyarsky @ScottSelikoff
    Module implications
    • java.base available by default
    • Can have “private” package
    • No circular dependencies
    57

    View full-size slide

  58. @JeanneBoyarsky @ScottSelikoff
    Sample
    58

    View full-size slide

  59. @JeanneBoyarsky @ScottSelikoff
    Question 11
    Which directive allows callers to access a
    package?
    A. exports
    B. opens
    C. reflects
    D. requires
    E. None of the above
    59

    View full-size slide

  60. @JeanneBoyarsky @ScottSelikoff
    Question 11
    Which directive allows callers to access a
    package?
    A. exports
    B. opens
    C. reflects
    D. requires
    E. None of the above
    60
    A

    View full-size slide

  61. @JeanneBoyarsky @ScottSelikoff
    Question 12
    How many modules are available by default?
    A. 0
    B. 1
    C. 2
    D. 3
    E. 4
    61

    View full-size slide

  62. @JeanneBoyarsky @ScottSelikoff
    Question 12
    How many modules are available by default?
    A. 0
    B. 1
    C. 2
    D. 3
    E. 4
    62
    B

    View full-size slide

  63. @JeanneBoyarsky @ScottSelikoff
    Question 13
    Which directive allows reflection?
    A. exports
    B. opens
    C. reflects
    D. requires
    E. None of the above
    63

    View full-size slide

  64. @JeanneBoyarsky @ScottSelikoff
    Question 13
    Which directive allows reflection?
    A. exports
    B. opens
    C. reflects
    D. requires
    E. None of the above
    64
    B

    View full-size slide

  65. @JeanneBoyarsky @ScottSelikoff
    Question 14
    Which is true of modules?
    A. A single class can be exported.
    B. A single package can be exported
    C. They require a module-def file
    D. They require a module-info file
    E. None of the above
    65

    View full-size slide

  66. @JeanneBoyarsky @ScottSelikoff
    Question 14
    Which is true of modules?
    A. A single class can be exported.
    B. A single package can be exported
    C. They require a module-def file
    D. They require a module-info file
    E. None of the above
    66
    B, D

    View full-size slide

  67. @JeanneBoyarsky @ScottSelikoff
    Question 15
    Which directive takes a module name?
    A. exports
    B. opens
    C. reflects
    D. requires
    E. None of the above
    67

    View full-size slide

  68. @JeanneBoyarsky @ScottSelikoff
    Question 15
    Which directive takes a module name?
    A. exports
    B. opens
    C. reflects
    D. requires
    E. None of the above
    68
    D

    View full-size slide

  69. @JeanneBoyarsky @ScottSelikoff 69
    Book Giveaway

    View full-size slide

  70. @JeanneBoyarsky @ScottSelikoff
    Question 16
    What is output
    when run
    java Output.java?
    A. 5
    B. 6
    C. Code does not compile
    D. Code throws an exception at runtime
    70

    View full-size slide

  71. @JeanneBoyarsky @ScottSelikoff
    Question 16
    What is output
    when run
    java Output.java?
    A. 5
    B. 6
    C. Code does not compile
    D. Code throws an exception at runtime
    71
    C

    View full-size slide

  72. @JeanneBoyarsky @ScottSelikoff
    Question 17
    What does the following output?
    Map map
    = Map.of(1, 2, 3, 4);
    System.out.println(map.get(2));
    A. 1
    B. 2
    C. null
    72
    D. Code does not
    compile
    E. Throws exception

    View full-size slide

  73. @JeanneBoyarsky @ScottSelikoff
    Question 17
    What does the following output?
    Map map
    = Map.of(1, 2, 3, 4);
    System.out.println(map.get(2));
    A. 1
    B. 2
    C. null
    73
    D. Code does not
    compile
    E. Throws exception
    C

    View full-size slide

  74. @JeanneBoyarsky @ScottSelikoff
    Question 18
    What does the following output?
    Map map
    = Map.of(1, 2, 3);
    System.out.println(map.get(2));
    A. 1
    B. 2
    C. null
    74
    D. Code does not
    compile
    E. Throws exception

    View full-size slide

  75. @JeanneBoyarsky @ScottSelikoff
    Question 18
    What does the following output?
    Map map
    = Map.of(1, 2, 3);
    System.out.println(map.get(2));
    A. 1
    B. 2
    C. null
    75
    D. Code does not
    compile
    E. Throws exception
    D

    View full-size slide

  76. @JeanneBoyarsky @ScottSelikoff
    Question 19
    What does the following output?
    List list = List.of(1, 2, 3);
    list.set(1, 0);
    System.out.println(list);
    A. [1, 0, 3]
    B. [1, 2, 3]
    C. null
    76
    D. Code does not
    compile
    E. Throws exception

    View full-size slide

  77. @JeanneBoyarsky @ScottSelikoff
    Question 19
    What does the following output?
    List list = List.of(1, 2, 3);
    list.set(1, 0);
    System.out.println(list);
    A. [1, 0, 3]
    B. [1, 2, 3]
    C. null
    77
    D. Code does not
    compile
    E. Throws exception
    E

    View full-size slide