[update] java12-example 示例完成
java13-example 示例完成
This commit is contained in:
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