Skip to content

Commit b5e2ae3

Browse files
committed
Java 14 Features
1 parent 2a5583b commit b5e2ae3

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hellokoding.java.java14;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class PatternMatchingForInstanceOf {
8+
public String instanceOf(Object o) {
9+
if (o instanceof Book) {
10+
Book b = (Book) o;
11+
if (b.id() == 1) {
12+
return b.title();
13+
}
14+
}
15+
16+
return "";
17+
}
18+
19+
public String instanceOfWithPatternMatching(Object o) {
20+
if (o instanceof Book b && b.id() == 1) {
21+
return b.title();
22+
} else {
23+
return "";
24+
}
25+
}
26+
27+
@Test
28+
public void testInstanceOf() {
29+
Book b = new Book(1, "Java 14");
30+
String s = instanceOfWithPatternMatching(b);
31+
32+
assertThat(s).isEqualTo("Java 14");
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.hellokoding.java.java14;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class Records {
8+
@Test
9+
public void testRecord() {
10+
Book b = new Book(1, "Java 14");
11+
int id = b.id();
12+
String title = b.title();
13+
14+
assertThat(id).isEqualTo(1);
15+
assertThat(title).isEqualTo("Java 14");
16+
}
17+
}
18+
19+
record Book(int id, String title) {
20+
}

0 commit comments

Comments
 (0)