7. 305:PatternMatchingforinstanceof(Preview)
Before
if (obj instanceof Double) {
Double d = (Double) obj;
// Processing with d
}
After
if (obj instanceof Double d) {
// Processing with d
}
thesamewayon switch (Undetermineddate)
switch (obj) {
case Integer i:
// want to do when Integer
case Double d:
// want to do when Double
}
NofurtherchangeinJava15(SecondPreview). 7
16. 4: String name = person.getName().toUpperCase();
public class Sample {
public static void main(String... args){
Person person = new Person();
String name = person.getName().toUpperCase();
}
static class Person {
String name = null;
Person() {
}
String getName() {
return name;
}
}
}
16
18. -XX:+ShowCodeDetailsInExceptionMessages
Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "String.toUpperCase()" // When
because the return value of "Sample$Person.getName()" is null // What
$ java -XX:+ShowCodeDetailsInExceptionMessages Sample
Exception in thread "main" java.lang.NullPointerException: Cann
at Sample.main(Sample.java:4)
18
19. Case:localvariable
3: String[] names = null;
4: names[1].toLowerCase();
$ java -XX:+ShowCodeDetailsInExceptionMessages LocalStringArray
Exception in thread "main" java.lang.NullPointerException:
Cannot load from object array // When
because "<local1>" is null // What
at LocalStringArray.main(LocalStringArray.java:4)
$ javac -g LocalStringArray.java // Add all debugging information
$ java -XX:+ShowCodeDetailsInExceptionMessages LocalStringArray
Exception in thread "main" java.lang.NullPointerException:
Cannot load from object array // When
because "names" is null // What
at LocalStringArray.main(LocalStringArray.java:4)
19