[update] java12-example 示例完成
java13-example 示例完成
This commit is contained in:
19
demo2/java12-example/README.md
Normal file
19
demo2/java12-example/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Java 12 新特性示例
|
||||
|
||||
本项目展示了Java 12的核心新特性及代码示例。
|
||||
|
||||
## 特性列表
|
||||
|
||||
### 1. Switch表达式(预览功能,JDK14正式添加)
|
||||
- **说明**:支持返回值的简化语法,使用`->`替代`:`,可直接赋值给变量
|
||||
- **启用方式**:编译/运行时需添加`--enable-preview --source 12`
|
||||
|
||||
### 2. 文件差异对比
|
||||
- **说明**:新增`Files.mismatch()`方法定位文件内容差异位置
|
||||
|
||||
### 3. Shenandoah垃圾回收器
|
||||
- **说明**:低延迟GC,需在JVM参数启用:`-XX:+UseShenandoahGC`
|
||||
|
||||
### 4. JVM常量API
|
||||
- **说明**:通过`ClassDesc`等接口标准化访问JVM常量池
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<compilerArgs>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
50
demo2/java12-example/src/main/java/Java12Main.java
Normal file
50
demo2/java12-example/src/main/java/Java12Main.java
Normal file
@@ -0,0 +1,50 @@
|
||||
import java.io.IOException;
|
||||
import java.lang.constant.ClassDesc;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* @author maxiaofeng
|
||||
* @date 2025/7/18 10:06
|
||||
*/
|
||||
public class Java12Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Java 12 Features Demo");
|
||||
switchExample();
|
||||
try {
|
||||
fileComparisonExample();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
constantAPIExample();
|
||||
}
|
||||
|
||||
// Switch表达式示例
|
||||
public static void switchExample() {
|
||||
int day = 3;
|
||||
String dayType = switch (day) {
|
||||
case 1, 2, 3, 4, 5 -> "Weekday";
|
||||
case 6, 7 -> "Weekend";
|
||||
default -> throw new IllegalArgumentException("Invalid day: " + day);
|
||||
};
|
||||
System.out.println("Switch表达式结果: " + dayType);
|
||||
}
|
||||
|
||||
// 文件对比示例
|
||||
public static void fileComparisonExample() throws IOException {
|
||||
Path file1 = Files.createTempFile("file1", ".txt");
|
||||
Path file2 = Files.createTempFile("file2", ".txt");
|
||||
Files.writeString(file1, "Hello");
|
||||
Files.writeString(file2, "World");
|
||||
// 输出第一个差异字节位置(0-based)
|
||||
System.out.println("文件差异位置: " + Files.mismatch(file1, file2));
|
||||
Files.delete(file1);
|
||||
Files.delete(file2);
|
||||
}
|
||||
|
||||
// 常量API示例
|
||||
public static void constantAPIExample() {
|
||||
ClassDesc c = ClassDesc.of("java.lang.String");
|
||||
System.out.println("类描述: " + c.displayName());
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
/**
|
||||
* @author maxiaofeng
|
||||
* @date 2025/7/18 10:06
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("hello java8.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user