File tree 4 files changed +14
-6
lines changed
Java14API/src/main/java/java14
4 files changed +14
-6
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class InstanceofPatternMatching {
4
4
public static void main (String [] args ) {
5
- Object user = ( Object ) "rishiraj" ;
5
+ Object user = "rishiraj" ;
6
6
7
7
// Older approach
8
8
if (user instanceof String ) {
9
9
String userName = (String ) user ;
10
10
System .out .println ("User name is: " + userName );
11
11
}
12
12
13
- // Java 14 approach and later
13
+ // " Java 14 and later" approach
14
14
if (user instanceof String userName ) {
15
15
System .out .println ("User name is: " + userName );
16
16
}
Original file line number Diff line number Diff line change 4
4
5
5
public class NullPointerExceptionPlus {
6
6
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
+ );
8
15
}
9
16
}
10
17
11
-
12
18
class UserProfile {
13
19
14
20
public static UserProfile newInstance () {
Original file line number Diff line number Diff line change @@ -10,8 +10,8 @@ public static void main(String[] args) {
10
10
}
11
11
12
12
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
15
15
return switch (inputNumber ) {
16
16
case 0 -> "zero" ;
17
17
case 1 ,3 ,5 ,7 ,9 -> "odd" ;
Original file line number Diff line number Diff line change 2
2
3
3
public class TextBlockVsString {
4
4
public static void main (String [] args ) {
5
+ // Older style
5
6
String strArbeitgeber = "Das sind die 10 besten Arbeitgeber:innen Deutschlands\n "
6
7
+ " Platz 1: dm-drogerie markt GmbH + Co. KG\n "
7
8
+ " Platz 2: Techniker Krankenkasse\n "
@@ -16,6 +17,7 @@ public static void main(String[] args) {
16
17
+ "Viel Glück!\n " ;
17
18
System .out .println (strArbeitgeber );
18
19
20
+ // "Java 14+" style
19
21
String tbArbeitgeber = """
20
22
Das sind die 10 besten Arbeitgeber:innen Deutschlands
21
23
Platz 1: dm-drogerie markt GmbH + Co. KG
You can’t perform that action at this time.
0 commit comments