Skip to content

Commit b053671

Browse files
committed
pattern matching in java 14
instanceof operator
1 parent cceaa20 commit b053671

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
<groupId>org.apache.maven.plugins</groupId>
1414
<artifactId>maven-compiler-plugin</artifactId>
1515
<configuration>
16-
<source>13</source>
17-
<target>13</target>
16+
<compilerArgument>--enable-preview</compilerArgument>
17+
<source>14</source>
18+
<target>14</target>
19+
<compilerArgs>--enable-preview</compilerArgs>
1820
</configuration>
1921
</plugin>
2022
</plugins>

src/main/java/challenge21_30/Challenge_23.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public static void main( String[] args ) {
3333
private <R extends WeekDay, T > T getUsingSwitchCase(R weekDay) {
3434
var t = switch((WeekDay)weekDay){
3535
case MONDAY -> (T) Integer.valueOf(4);
36-
case TUESDAY, WEDNESDAY, THURSDAY -> {
37-
System.out.println("Every day is beer day for homer");
38-
break (Integer.valueOf(129) == 129 ? 5 : 7);
39-
}
36+
// case TUESDAY, WEDNESDAY, THURSDAY -> {
37+
// System.out.println("Every day is beer day for homer");
38+
// break (Integer.valueOf(129) == 129 ? 5 : 7);
39+
// }
4040
case FRIDAY -> (T) Double.valueOf(4);
4141
case SATURDAY, SUNDAY -> (T) (Object) "6" == new String("6") ? 2 : 4;
4242
default -> new IllegalStateException("Non existent day.");

src/main/java/others/youtube/BeyondJava8.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,53 @@
88
/**
99
* Life Beyond Java 8, by Trisha Gee / JetBrains Technology Day for Java (2020)
1010
* https://www.youtube.com/watch?v=gKestt55Q4M
11+
* @todo java 13 and above
1112
*/
1213
public class BeyondJava8 {
1314

1415
public static void main( String[] args ) {
1516
//unmodifiableList();
1617
//unmodifiableMap();
1718
//collectToUnmodifiableList();
18-
predicateNot();
19+
//predicateNot();
20+
//newSwitchStatement(new Random().nextInt());
21+
//textBlocks();
22+
Person p1 = new Employee("zaki");
23+
instanceOfInJava14(p1);
24+
}
25+
26+
private static void instanceOfInJava14(Person person) {
27+
28+
if (person instanceof Employee)
29+
{
30+
Employee employee = ((Employee) person);
31+
System.out.println("employee.getEmployeeName() = " + employee.getEmployeeName());
32+
}
33+
//in Java 14, we can write the above code as follows
34+
35+
if (person instanceof Employee employee)
36+
{
37+
System.out.println("employee.getEmployeeName() = " + employee.getEmployeeName());
38+
}
39+
}
1940

41+
//java 13, for html, sql queries, json ......
42+
private static void textBlocks() {
43+
String multiLinesText =
44+
"""
45+
Hello Brother, I'm using Java14;
46+
""";
47+
System.out.println("multiLinesText = " + multiLinesText);
48+
}
49+
50+
// the new switch syntax
51+
private static void newSwitchStatement(int i) {
52+
String value = switch (i){
53+
case 0 -> "0";
54+
case 1, 2 -> "1";
55+
default -> "NOT in [0-2]";
56+
};
57+
System.out.println("value = " + value);
2058
}
2159

2260
private static void predicateNot() {
@@ -53,5 +91,32 @@ public static void unmodifiableList(){
5391
List<String> stringList2 = List.of("A", "B");
5492
stringList2.set(0, "C");
5593
}
94+
}
95+
96+
97+
class Person{
98+
String name;
99+
100+
public Person(String name) {
101+
this.name = name;
102+
}
103+
104+
@Override
105+
public String toString() {
106+
return "Person{" +
107+
"name='" + name + '\'' +
108+
'}';
109+
}
110+
}
111+
112+
class Employee extends Person{
113+
public Employee(String name) {
114+
super(name);
115+
}
116+
public String getEmployeeName(){
117+
return "Employee{" +
118+
"name='" + this.name + '\'' +
119+
'}';
120+
}
56121

57122
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package others.youtube;
2+
3+
/**
4+
* Let's Get Lazy, by Venkat Subramaniam / JetBrains Technology Day for Java (2020)
5+
* https://www.youtube.com/watch?v=PICHx2at46s
6+
*/
7+
public class LetsGetLazy {
8+
9+
public static void main( String[] args ) {
10+
System.out.println();
11+
}
12+
}

0 commit comments

Comments
 (0)