Skip to content

Commit 15952a5

Browse files
committed
feat: add java 20 demo
1 parent 14ed896 commit 15952a5

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @author niulang
3+
*/
4+
public class JEP433SwitchTest {
5+
public static void main(String[] args) {
6+
Object obj = 123;
7+
System.out.println(matchOld(obj)); // 是个数字
8+
System.out.println(matchNew(obj)); // 是个数字
9+
obj = "wdbyte.com";
10+
System.out.println(matchOld(obj)); // 是个字符串,长度大于2
11+
System.out.println(matchNew(obj)); // 是个字符串,长度大于2
12+
}
13+
14+
/**
15+
* 老代码
16+
*
17+
* @param obj
18+
* @return
19+
*/
20+
public static String matchOld(Object obj) {
21+
if (obj == null) {
22+
return "数据为空";
23+
}
24+
if (obj instanceof String) {
25+
String s = obj.toString();
26+
if (s.length() > 2) {
27+
return "是个字符串,长度大于2";
28+
}
29+
if (s.length() <= 2) {
30+
return "是个字符串,长度小于等于2";
31+
}
32+
}
33+
if (obj instanceof Integer) {
34+
return "是个数字";
35+
}
36+
throw new IllegalStateException("未知数据:" + obj);
37+
}
38+
39+
/**
40+
* 新代码
41+
*
42+
* @param obj
43+
* @return
44+
*/
45+
public static String matchNew(Object obj) {
46+
String res = switch (obj) {
47+
case null -> "数据为空";
48+
case String s when s.length() > 2 -> "是个字符串,长度大于2";
49+
case String s when s.length() <= 2 -> "是个字符串,长度小于等于于2";
50+
case Integer i -> "是个数字";
51+
default -> throw new IllegalStateException("未知数据:" + obj);
52+
};
53+
return res;
54+
}
55+
56+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import jdk.incubator.concurrent.ScopedValue;
2+
3+
/**
4+
* --add-modules jdk.incubator.concurrent
5+
*
6+
* @date 2023/05/04
7+
*/
8+
public class Jep429ScopedValueTest {
9+
final static ScopedValue<String> SCOPED_VALUE = ScopedValue.newInstance();
10+
11+
public static void main(String[] args) {
12+
// 创建线程
13+
Thread thread1 = new Thread(Jep429ScopedValueTest::handle);
14+
Thread thread2 = new Thread(Jep429ScopedValueTest::handle);
15+
String str = "hello wdbyte";
16+
// 传入线程里使用的字符串信息
17+
ScopedValue.where(SCOPED_VALUE, str).run(thread1);
18+
ScopedValue.where(SCOPED_VALUE, str).run(thread2);
19+
// 执行完毕自动清空,这里获取不到了。
20+
System.out.println(SCOPED_VALUE.orElse("没有信息"));
21+
}
22+
23+
public static void handle() {
24+
String result = SCOPED_VALUE.get();
25+
System.out.println(result);
26+
}
27+
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author niulang
3+
*/
4+
public class Jep432RecordAndInstance {
5+
public static void main(String[] args) {
6+
ColoredPoint coloredPoint1 = new ColoredPoint(new Point(0, 0), Color.RED);
7+
ColoredPoint coloredPoint2 = new ColoredPoint(new Point(1, 1), Color.GREEN);
8+
Rectangle rectangle = new Rectangle(coloredPoint1, coloredPoint2);
9+
printUpperLeftColoredPoint(rectangle);
10+
}
11+
12+
static void printUpperLeftColoredPoint(Rectangle r) {
13+
if (r instanceof Rectangle(ColoredPoint ul, ColoredPoint lr)) {
14+
System.out.println(ul.c());
15+
}
16+
}
17+
}
18+
19+
record Point(int x, int y) {}
20+
enum Color { RED, GREEN, BLUE }
21+
record ColoredPoint(Point p, Color c) {}
22+
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @author niulang
3+
* @date 2023/05/04
4+
*/
5+
public class RecordTest {
6+
public static void main(String[] args) {
7+
Dog dog = new Dog("name", 1);
8+
System.out.println(dog.name());
9+
System.out.println(dog.age());
10+
}
11+
}
12+
13+
record Dog(String name, Integer age) {
14+
}

0 commit comments

Comments
 (0)