Skip to content

Commit 36eb382

Browse files
committed
Minor refactoring and commenting.
1 parent e350ca0 commit 36eb382

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

Java14API/src/main/java/java14/InstanceofPatternMatching.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class InstanceofPatternMatching {
44
public static void main(String[] args) {
5-
Object user = (Object) "rishiraj";
5+
Object user = "rishiraj";
66

77
// Older approach
88
if (user instanceof String) {
99
String userName = (String) user;
1010
System.out.println("User name is: " + userName);
1111
}
1212

13-
// Java 14 approach and later
13+
// "Java 14 and later" approach
1414
if (user instanceof String userName) {
1515
System.out.println("User name is: " + userName);
1616
}

Java14API/src/main/java/java14/NullPointerExceptionPlus.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
public class NullPointerExceptionPlus {
66
public static void main(String[] args) {
7-
System.out.println(UserProfile.newInstance().getRoleClass().getPrivileges().toString());
7+
System.out.println(
8+
UserProfile.newInstance().getRoleClass().getPrivileges().toString()
9+
/*The following descriptive NPE message is shown in Java 14 and later versions:
10+
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "java14.RoleClass.getPrivileges()" is null
11+
at java14.NullPointerExceptionPlus.main(NullPointerExceptionPlus.java:8)
12+
13+
*/
14+
);
815
}
916
}
1017

11-
1218
class UserProfile {
1319

1420
public static UserProfile newInstance() {

Java14API/src/main/java/java14/SwitchExpressions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public static void main(String[] args) {
1010
}
1111

1212
static String stateNumberClass(int inputNumber) {
13-
// See the beauty and the brevity of code
14-
// break; is not needed to exit the switch when a match has been made
13+
// See the beauty and the brevity of code:
14+
// "break;" is not needed to exit the switch construct when a matching block has been run
1515
return switch(inputNumber) {
1616
case 0 -> "zero";
1717
case 1,3,5,7,9 -> "odd";

Java14API/src/main/java/java14/TextBlockVsString.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public class TextBlockVsString {
44
public static void main(String[] args) {
5+
// Older style
56
String strArbeitgeber = "Das sind die 10 besten Arbeitgeber:innen Deutschlands\n"
67
+ " Platz 1: dm-drogerie markt GmbH + Co. KG\n"
78
+ " Platz 2: Techniker Krankenkasse\n"
@@ -16,6 +17,7 @@ public static void main(String[] args) {
1617
+ "Viel Glück!\n";
1718
System.out.println(strArbeitgeber);
1819

20+
// "Java 14+" style
1921
String tbArbeitgeber = """
2022
Das sind die 10 besten Arbeitgeber:innen Deutschlands
2123
Platz 1: dm-drogerie markt GmbH + Co. KG

0 commit comments

Comments
 (0)