Become one of the first Java 17 developers in the world
The key to being certified early is to learn the material before the objectives are even announced. Here's a guide to the new topics based on my guesses of what will be on an exam
my books. • Some of the material in this presentation may appear in our upcoming certification books. • Oracle can announce anything (I don’t work for Oracle.) 6
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 8
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 13
<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 15 21
String option2 = "a\nb\nc\n".indent(3) ; String option3 = "" " a b c """.indent(3) ; String option4 = "" " a b c """ ; Which do you like best? Also normalizes (bye \r) 12 24
this text block? String sql = "" " select * from mytabl e where weather = 'snow' ; """ ; A. Has incidental whitespace B. Has essential whitespace C. Both A and B D. Does not compile 27
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 28
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 29
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 30
be removed without changing the behavior? String sql = "" " select \"name\" from mytable \ where value = '\"\"" ' """; A. 2 B. 3 C. 4 D. Does not compile 36
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 38
"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 44
; } 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 46
; 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) 48
obj) { return switch (obj) { case Integer i -> i ; case Double d -> d.intValue() ; case String s -> Integer.parseInt(s) ; default -> throw new IllegalArgumentException("unknown type") ; } ; } Reminder: Syntax can change 56
obj) { switch (obj) { case Integer i && i % 2 == 1 -> System.out.println("odd") ; case Integer i && i % 2 == 0 -> System.out.println(“even") ; default -> System.out.println("not an int") ; } ; } Reminder: Feature can still change 57
ch = 'b' ; int count = 0 ; switch (ch) { case 'a' -> count++ ; case 'b' -> count+=2 ; case 'c' -> count+=3 ; } System.out.println(count) ; 58 C. 5 D. Does not compile A. 1 B. 2
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) ; 59 C. 3 D. 4 A. 1 B. 2
changing to make this code compile? char ch = 'b' ; int value = switch (ch) { case 'a' : yield 1 ; case 'b' : { yield 2; } case 'c' -> yield 3 ; default -> 4 ; } ; System.out.println(value) ; 60 C. 3 D. 4 A. 1 B. 2
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) ; 61 C. Either A or B D. None of the above A. int B. Object
output? char ch = 'b' ; switch (ch) { case 'a' -> System.out.println(1) ; case 'b' -> System.out.println(2) ; case 'c' -> { System.out.println(3); } } ; A. 1 B. 2 C. 3 D. Does not compile 62
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) ; 63 C. y694 D. Does not compile A. x694 B. xy694
in scope? Object robot = "694" ; if (robot instanceof String s) { // line 1 } if (robot instanceof int i) { // line 2 } // line 3 64 C. 1, 2 and 3 D. Does not compile A. 1 B. 1 and 3
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 implemente d } 65 C. equals() does not compile A. equals() is correct B. equals() is incorrect
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()) { } 66 C. 2 D. 3 A. 0 B. 1
class Sword { int length = 8 ; public void printLength(Object x) { if (x instanceof Integer length) { length = 2 ; } System.out.println(length) ; } } 67 C. 8 D. Does not compile A. 2 B. 3
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 69
numPages) { } New type Automatically get * final record * private final instance variables * public accessors * constructor taking both fields * equals * hashCode * no instance initializers 75
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] 76
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 78
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 80
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 83
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)) ; } 85 C. 2 D. None of the above A. 0 B. 1
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()) ; } 86 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()) ; } 87 C. Does not compile D. None of the above A. chicken B. CHICKEN
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 ""; } } 88 C. 3 D. 4 A. 1 B. 2
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)) ; } 89 C. 0 D. Does not compile A. Negative # B. Positive #
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()) ; } 90 C. Does not compile D. None of the above A. true B. false
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 91
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 92
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 93
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 96
result = "" ; for (Integer num : list) { if (! result.isBlank()) { result += " " ; } result += num ; } return result ; } public String joinStream(List<Integer> list) { return list.stream( ) .map(Object::toString ) .collect(Collectors.joining(" ")) ; } 105
public static void main(String[] args) { long count = Strea m .iterate(1; i-> i< 10; i-> i+2).count() ; System.out.println(count) ; } A. 0 B. 5 C. Does not compile D. None of the above 120
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-10 C. Does not compile D. None of the above 121
var map = Stream.generate(() -> 1 ) .limit(5 ) .collect(Collectors.partitioningBy ( x -> x % 2 ==0)) ; System.out.println(map) ; A. {false=[1, 1, 1, 1, 1]} B. {false=[1, 1, 1, 1, 1], true=[]} C. Does not compile D. None of the above 122
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 125
the blank to print false? (Choose all that apply) var s = Stream.generate(() -> "meow") ; var match = ________________(String::isEmpty) ; System.out.println(match) ; A. anyMatch B. allMatch C. noneMatch D. findFirst 126
blank to compile but cause the program to hang?(Choose all that apply) var s = Stream.generate(() -> "meow") ; var match = ________________(String::isEmpty) ; System.out.println(match) ; A. anyMatch B. allMatch C. noneMatch D. findFirst 127
of the following? var x = Stream.of("a", "b", "c" ) .filter(c -> c != "b") ; System.out.println(x) ; A. [b] B. [a, c] C. [a, b, c] D. Does not compile E. None of the above 128
the blank, then the output is_____ (Choose two) var x = Stream.of("a", "bb", "_________" ) .collect(Collectors.toMap ( String::length, Function.identity())) ; System.out.println(x) ; A. cc, {1=a, 2=bb, 3=cc} B. cc, a stack trace C. ccc, {1=a, 2=bb, 3=ccc} D. ccc, a stack trace 129
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 131
String %d Decimal integer (no dot) %c Char %f Float (decimal) %n New line Many more out of scope. Examples: • %e - scientific notation • %t - time • %S - converts to all uppercase 134
constant = Locale.JAPANESE ; Locale lang = new Locale("en") ; Locale country = new Locale("en", "US") ; Locale invalid = new Locale("US") ; Invalid locales don’t throw an exception, but don’t match 141
open=The zoo is ope n // in main metho d Locale french = new Locale("fr", "FR") ; ResourceBundle rb = ResourceBundle.getBundle("Zoo", french) ; System.out.println(rb.getString("open")) ; Le zoo est ouvert # Zoo_fr.propertie s hello=Bonjou r open=Le zoo est ouver t 143
Requested locale Zoo_fr.properties Requested language Zoo_en_US.properties Default locale Zoo_en.properties Default language Zoo.properties Default bundle throws exception No matches Once finds bundle, can only get values from it or parent 144
zoo is ope n // in main metho d ResourceBundle rb = ResourceBundle.getBundle("Zoo") ; String format = rb.getString("hello") ; System.out.println(MessageFormat.format ( format, "Jeanne")) ; Hello Jeanne # Zoo_fr.propertie s hello=Bonjour {0 } open=Le zoo est ouver t 145
to be changed to print: 2K 3.14? CompactNumberFormat fmt = new CompactNumberFormat() ; System.out.println(fmt.format(2000) + " %.2d".formatted(3.14)) ; A. 1 B. 2 C. 3 D. 4 148
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 150
will be the first bundle to match? Locale.setDefault(Locale.US) ; ResourceBundle rb = new ResourceBundle("Zoo" , new Locale("fr")) ; A. Zoo_en.properties B. Zoo_fr_FR.properties C. Zoo.properties D. None of the above 151
will be the first bundle to match? Locale.setDefault(Locale.US) ; ResourceBundle rb = ResourceBundle.getBundle("Zoo" , new Locale("fr")) ; A. Zoo_en.properties B. Zoo_US.properties C. Zoo.properties D. None of the above 152
in this example. Which file will Java look at first if there is no match in that file? ResourceBundle rb = ResourceBundle.getBundle("Zoo" , new Locale("fr")) ; A. Zoo_en_US.properties B. Zoo_fr_FR.properties C. Zoo.properties D. None of the above 153
output? String format = "{1} < {0}” ; System.out.println ( MessageFormat.format(3, 5)) ; A. 3 < 5 B. 5 < 3 C. {2} < 3 D. {2} < 5 E. None of the above 155
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 157
// lots of code her e System.out.println(path.toAbsolutePath()) ; % java11 HelpfulNullPointer.java Exception in thread "main" java.lang.NullPointerExceptio n 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 nul l at HelpfulNullPointer.main(HelpfulNullPointer.java:9 ) 14 161
; APPEND Adds to file CREATE Creates new file if doesn’t exist CREATE_NEW Creates new file if exists. Otherwise fails Defaults to creating if doesn’t exist and overwriting if does 170
createDirectory() createDirectories() The later adds interm folders copy() Copy path/input stream to file/directory move() Move between paths delete() deleteIfExists() The former throws exception newBufferedReader() newBufferedWriter() Interact with legacy I/O walk() Visit all files in tree A number of these take optional varargs to customize 172
c: or … getParent() One level up getRoot() Top level normalize() Simplifies . and .. relativize(Path) Relative path from object to parameter (both must be absolute or relative) resolve(Path) Add parameter to path of object (if absolute path passed in, object ignored) 173
output? Path clock = new Path("time/device/../clock") ; Path phone = new Path("time/phone") ; System.out.println(clock.relativize(phone)) ; A. phone B. ../phone C. time/device/phone D. time/device/../clock/..phone E. None of the above 176
output? Path clock = Paths.get(“time/device/../clock”) ; Path phone = Path.of(“time/phone") ; System.out.println(clock.relativize(phone)) ; A. phone B. ../phone C. time/device/phone D. time/device/../clock/..phone E. None of the above 177
path = Path.of("file") ; System.out.println(Files.mismatch(path, path)) ; A. If the file exists, prints -1 B. If the file exists, throws exception C. If the file does not exist, prints -1 D. If the file does not exist, throws exception E. Does not compile 178
file1 contains “howdy”? Path path1 = Path.of("file1") ; Path path2 = Path.of("file2") ; System.out.println(Files.mismatch(path1, path2)) ; A. If file2 contains “howdy”, prints -1 B. If file2 contains “howdy”, prints 0 C. If file2 does not exist, prints -1 D. If file2 does not exist, throws exception E. Does not compile 179
file1 contains “howdy”? Path path1 = Path.of("file1") ; Path path2 = Path.of("file2") ; System.out.println(Files.mismatch(path1, path2)) ; A. If file2 contains “hello”, prints -1 B. If file2 contains “hello”, prints 0 C. If file2 contains “hello”, prints 1 D. If file2 contains “uh”, prints -1 E. If file2 contains “uh”, prints 0 180
in this code? public static void main(String[] args) { Files.lines(Path.of("lamp") ) .filter(String::isEmpty ) .collect(Collectors.toSet() ) .forEach(System.out::println) ; } A. 0 B. 1 C. 2 D. 3 181
this code? Console console = System.console() ; char[] secret = console.readPassword() ; System.out.println(secret) ; A. Guaranteed to print password B. Guaranteed not to print password C. Guaranteed to complete successfully D. Does not compile E. May throw an unchecked exception 182
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 187
feeding/zoo/animal/feeding/*.java feeding/module-info.jav a java --module-path feeding --module zoo.animal.feeding/zoo.animal.feeding.Tas k jar -cvf mods/zoo.animal.feeding.jar -C feeding/ . module name fully qualified class name -p is module path -m is module
module private Accessible only within class No access default (package/ package-private) Accessible only within package No access protected Accessible only within package or to subclasses Accessible to subclasses only if exported public Accessible to all classes Accessible only if package exported
modules the target exports • Cannot have duplicate requires statements (or requires and requires transitive) • Can require something that is in the other modules exports list 203
service provides zoo.staff.ZooApi with zoo.staff.ZooImpl uses Uses a service uses zoo.staff.ZooImpl opens Allows reflection opens zoo.animal.talks.schedule; opens zoo.animal.talks.media to zoo.staff;
valid module? zoo.staf f |---zo o |-- staf f |-- Vet.jav a A. Yes if module-info was added under zoo.staff B. Yes if module-info was added under zoo C. Yes if module-info was added under staff D. None of these make it valid 206
to compile jav a ____ zoo.animal.talks/zoo/animal/talks/Peacock s ____ module s A. -d and -m B. -d and -p C. -m and -d D. -m and -p E. None of the above 209
this module? (Choose two) module com.food.supplier { } A. All packages are automatically exported B. No packages are automatically exported C. A main method can be run D. A main method cannot be run 210
command to run a program in a module? (Choose all) A. java -p x -m x/x B. java -p x-x -m x/x C. java -p x -m x-x/x D. java -p x -m x/x-x E. java -p x -m x.x F. java -p x.x -m x.x 211
the blank? module __________ { exports com.unicorn.horn ; exports com.unicorn.magic ; } A. com B. com.unicorn C. com.unicorn.horn D. The code does not compile E. None of these is a good choice 212
to have a compiler error? 1: module snake { 2: exports com.snake.tail ; 3: exports com.snake.fangs to bird ; 4: requires skin ; 5: requires transitive skin ; 6: } A. 1 B. 3 C. 5 D. None of the above 213
2 Switch expressions & Pattern matching 3 Records and Sealed classes 4 Streams including Collectors.teeing 5 Localization/Formatting including CompactNumberFormatting 6 I/O including Helpful NullPointers 7 Modules 8 Other topics + open Q&A 216
the resources are ResultSets? try (rs1, rs2) { } A. rs1 is closed before rs2 B. rs2 is closed before rs1 C. Neither is closed since declared before try D. The code does not compile 237
the resources are ResultSets? try (rs1; rs2) { } A. rs1 is closed before rs2 B. rs2 is closed before rs1 C. Neither is closed since declared before try D. The code does not compile 238
of the following? 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 D. 1 E. None of the above 239
of the following? 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 D. 1.0 E. None of the above 240
of the following? var path = Paths.get("file") ; var reader = Files.newBufferedReader(path) ; reader = Files.newBufferedReader(path) ; try(reader) { // read fil e } A. reader is closed B. reader remains open C. None; the code does not compile 241
of the following? 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 D. 1 E. None of the above 242
•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""") ; } } 247 java17 Fun.java Wear 🥽 and 🥼 when using a 🧪