diff --git a/demo2/java12-example/README.md b/demo2/java12-example/README.md new file mode 100644 index 0000000..3cacba9 --- /dev/null +++ b/demo2/java12-example/README.md @@ -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常量池 + diff --git a/demo2/java12-example/pom.xml b/demo2/java12-example/pom.xml index 2b1e8e8..03b3f54 100644 --- a/demo2/java12-example/pom.xml +++ b/demo2/java12-example/pom.xml @@ -24,6 +24,9 @@ ${maven.compiler.source} ${maven.compiler.target} + + --enable-preview + diff --git a/demo2/java12-example/src/main/java/Java12Main.java b/demo2/java12-example/src/main/java/Java12Main.java new file mode 100644 index 0000000..5904a77 --- /dev/null +++ b/demo2/java12-example/src/main/java/Java12Main.java @@ -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()); + } +} diff --git a/demo2/java13-example/README.md b/demo2/java13-example/README.md new file mode 100644 index 0000000..9698ec9 --- /dev/null +++ b/demo2/java13-example/README.md @@ -0,0 +1,26 @@ +# Java 13 新特性示例 + +本项目展示了Java 13的核心新特性及代码示例。 + +## 特性列表 + +### 1. 文本块(Text Blocks)- 预览功能 +- **说明**:使用`"""`定义多行字符串,避免转义字符,提高可读性 +- **示例位置**:`src/main/java/features/TextBlocksExample.java` + +### 2. Switch表达式增强 - 预览功能 +- **说明**:JDK 12预览功能的改进版本,支持yield返回值 +- **示例位置**:`src/main/java/features/SwitchExpressionsExample.java` +- **启用方式**:编译/运行时需添加`--enable-preview` + +### 3. Socket API 重新实现 +- **说明**:使用NIO实现更简单、更维护的Socket API +- **示例位置**:`src/main/java/features/SocketAPIExample.java` + +### 4. ZGC(Z Garbage Collector)增强 +- **说明**:将ZGC的使用范围从Linux扩展到macOS和Windows(实验性) +- **启用方式**:JVM参数`-XX:+UseZGC` + +### 5. 动态CDS Archives +- **说明**:简化了类数据共享的使用,提高启动性能 +- **使用方式**:通过`-XX:ArchiveClassesAtExit`和`-XX:SharedArchiveFile`参数使用 diff --git a/demo2/java13-example/pom.xml b/demo2/java13-example/pom.xml index 0f777b1..65d5ceb 100644 --- a/demo2/java13-example/pom.xml +++ b/demo2/java13-example/pom.xml @@ -24,6 +24,17 @@ ${maven.compiler.source} ${maven.compiler.target} + + --enable-preview + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M7 + + --enable-preview diff --git a/demo2/java13-example/src/main/java/Java13Main.java b/demo2/java13-example/src/main/java/Java13Main.java new file mode 100644 index 0000000..431ed49 --- /dev/null +++ b/demo2/java13-example/src/main/java/Java13Main.java @@ -0,0 +1,27 @@ +import features.DynamicCDSExample; +import features.SocketAPIExample; +import features.SwitchExpressionsExample; +import features.TextBlocksExample; +import features.ZGCExample; + +/** + * @author maxiaofeng + * @date 2025/7/18 10:06 + */ +public class Java13Main { + public static void main(String[] args) { + System.out.println("Java 13 新特性示例"); + System.out.println("预览功能需要添加 --enable-preview 参数才能运行"); + System.out.println("=================="); + System.out.println("1. Text Blocks - 文本块"); + TextBlocksExample.exec(); + System.out.println("2. Switch Expressions - Switch表达式"); + SwitchExpressionsExample.exec(); + System.out.println("3. Socket API - Socket API重新实现"); + SocketAPIExample.exec(); + System.out.println("4. ZGC - Z垃圾收集器增强"); + ZGCExample.exec(); + System.out.println("5. Dynamic CDS Archives - 动态类数据共享"); + DynamicCDSExample.exec(); + } +} \ No newline at end of file diff --git a/demo2/java13-example/src/main/java/Main.java b/demo2/java13-example/src/main/java/Main.java deleted file mode 100644 index bbb3c32..0000000 --- a/demo2/java13-example/src/main/java/Main.java +++ /dev/null @@ -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."); - } -} diff --git a/demo2/java13-example/src/main/java/features/DynamicCDSExample.java b/demo2/java13-example/src/main/java/features/DynamicCDSExample.java new file mode 100644 index 0000000..3bfaa5b --- /dev/null +++ b/demo2/java13-example/src/main/java/features/DynamicCDSExample.java @@ -0,0 +1,37 @@ +package features; + +public class DynamicCDSExample { + public static void exec() { + System.out.println("Dynamic CDS Archives 示例"); + System.out.println("类数据共享(CDS)可以提高JVM启动速度和减少内存使用"); + System.out.println(); + System.out.println("Java 13 增强:"); + System.out.println("- 简化了CDS的使用"); + System.out.println("- 支持动态创建归档文件"); + System.out.println(); + System.out.println("使用方式:"); + System.out.println("1. 运行应用时创建归档文件:"); + System.out.println(" java -XX:ArchiveClassesAtExit=my_archive.jsa MyApplication"); + System.out.println("2. 使用归档文件运行应用:"); + System.out.println(" java -XX:SharedArchiveFile=my_archive.jsa MyApplication"); + System.out.println(); + System.out.println("优势:"); + System.out.println("- 减少启动时间"); + System.out.println("- 减少内存占用"); + System.out.println("- 多个JVM实例可以共享只读类数据"); + + // 演示加载一些常用类 + try { + Class stringClass = Class.forName("java.lang.String"); + Class listClass = Class.forName("java.util.List"); + Class hashMapClass = Class.forName("java.util.HashMap"); + + System.out.println("\n加载的类:"); + System.out.println("- " + stringClass.getName()); + System.out.println("- " + listClass.getName()); + System.out.println("- " + hashMapClass.getName()); + } catch (ClassNotFoundException e) { + System.out.println("类加载异常: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/demo2/java13-example/src/main/java/features/SocketAPIExample.java b/demo2/java13-example/src/main/java/features/SocketAPIExample.java new file mode 100644 index 0000000..482a003 --- /dev/null +++ b/demo2/java13-example/src/main/java/features/SocketAPIExample.java @@ -0,0 +1,47 @@ +package features; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.StandardSocketOptions; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +public class SocketAPIExample { + public static void exec() { + try { + // 创建服务器Socket通道 (使用NIO实现) + ServerSocketChannel serverSocket = ServerSocketChannel.open(); + serverSocket.bind(new InetSocketAddress(8080)); + serverSocket.setOption(StandardSocketOptions.SO_REUSEADDR, true); + + System.out.println("服务器启动在端口 8080"); + + // 演示新API - 获取本地地址 + System.out.println("服务器地址: " + serverSocket.getLocalAddress()); + + // 模拟客户端连接 + new Thread(() -> { + try { + Thread.sleep(1000); // 等待服务器启动 + SocketChannel client = SocketChannel.open(); + client.connect(new InetSocketAddress("localhost", 8080)); + System.out.println("客户端连接成功"); + client.close(); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + + // 接受连接 + SocketChannel clientSocket = serverSocket.accept(); + System.out.println("接受客户端连接: " + clientSocket.getRemoteAddress()); + + // 关闭连接 + clientSocket.close(); + serverSocket.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/demo2/java13-example/src/main/java/features/SwitchExpressionsExample.java b/demo2/java13-example/src/main/java/features/SwitchExpressionsExample.java new file mode 100644 index 0000000..ac6e9d0 --- /dev/null +++ b/demo2/java13-example/src/main/java/features/SwitchExpressionsExample.java @@ -0,0 +1,46 @@ +package features; + +public class SwitchExpressionsExample { + public static void exec() { + // 传统switch语句 + int day = 3; + String dayType; + switch (day) { + case 1: + case 2: + case 3: + case 4: + case 5: + dayType = "Weekday"; + break; + case 6: + case 7: + dayType = "Weekend"; + break; + default: + dayType = "Invalid"; + } + System.out.println("传统switch: " + dayType); + + // Java 13 switch表达式 (需要--enable-preview参数) + String result = switch (day) { + case 1, 2, 3, 4, 5 -> "Weekday"; + case 6, 7 -> "Weekend"; + default -> "Invalid"; + }; + System.out.println("Switch表达式: " + result); + + // 使用yield返回值 (需要--enable-preview参数) + String dayName = switch (day) { + case 1: yield "Monday"; + case 2: yield "Tuesday"; + case 3: yield "Wednesday"; + case 4: yield "Thursday"; + case 5: yield "Friday"; + case 6: yield "Saturday"; + case 7: yield "Sunday"; + default: yield "Invalid day"; + }; + System.out.println("Day name: " + dayName); + } +} \ No newline at end of file diff --git a/demo2/java13-example/src/main/java/features/TextBlocksExample.java b/demo2/java13-example/src/main/java/features/TextBlocksExample.java new file mode 100644 index 0000000..d9b7dcc --- /dev/null +++ b/demo2/java13-example/src/main/java/features/TextBlocksExample.java @@ -0,0 +1,38 @@ +package features; + +public class TextBlocksExample { + public static void exec() { + // 传统方式定义多行字符串 + String jsonOld = "{\n" + + " \"name\": \"Java 13\",\n" + + " \"version\": 13,\n" + + " \"preview\": true\n" + + "}"; + + // 使用文本块(预览功能) + String json = """ + { + "name": "Java 13", + "version": 13, + "preview": true + } + """; + + System.out.println("传统方式:"); + System.out.println(jsonOld); + System.out.println("\n文本块方式:"); + System.out.println(json); + + // 文本块的其他用法 + String html = """ + + +

Hello, Java 13!

+ + + """; + + System.out.println("\nHTML示例:"); + System.out.println(html); + } +} \ No newline at end of file diff --git a/demo2/java13-example/src/main/java/features/ZGCExample.java b/demo2/java13-example/src/main/java/features/ZGCExample.java new file mode 100644 index 0000000..3d10912 --- /dev/null +++ b/demo2/java13-example/src/main/java/features/ZGCExample.java @@ -0,0 +1,32 @@ +package features; + +public class ZGCExample { + public static void exec() { + System.out.println("ZGC (Z Garbage Collector) 示例"); + System.out.println("ZGC是为低延迟应用设计的垃圾收集器"); + System.out.println("特点:"); + System.out.println("1. 暂停时间不超过10ms"); + System.out.println("2. 暂停时间不会随堆大小或活跃对象数量增加而增加"); + System.out.println("3. 支持TB级别的堆大小"); + System.out.println(); + System.out.println("使用方式:"); + System.out.println("在JVM启动参数中添加: -XX:+UseZGC"); + System.out.println(); + System.out.println("注意:"); + System.out.println("在Java 13中,ZGC在macOS和Windows上仍为实验性功能"); + System.out.println("生产环境建议在Linux上使用"); + + // 演示内存分配压力 + System.out.println("开始分配大对象..."); + for (int i = 0; i < 1000; i++) { + // 创建大对象以触发垃圾回收 + byte[] array = new byte[1024 * 1024]; // 1MB + // 短暂使用后丢弃引用,使其可被回收 + } + System.out.println("对象分配完成"); + + // 获取垃圾收集器信息 + String gcName = System.getProperty("java.vm.name"); + System.out.println("当前JVM: " + gcName); + } +} \ No newline at end of file diff --git a/demo2/java14-example/README.md b/demo2/java14-example/README.md new file mode 100644 index 0000000..7563f94 --- /dev/null +++ b/demo2/java14-example/README.md @@ -0,0 +1,35 @@ +Java 14 新特性示例 +--- + +本项目展示了Java 14的核心新特性及代码示例。 + +## 特性列表 + +### 1. Record(记录类)- 预览功能 +- **说明**:提供一种紧凑的语法来声明只读数据载体,减少样板代码 +- **示例位置**:`src/main/java/features/RecordExample.java` +- **启用方式**:编译/运行时需添加`--enable-preview` + +### 2. Pattern Matching for instanceof(instanceof模式匹配)- 预览功能 +- **说明**:简化 instanceof 操作,自动进行类型转换 +- **示例位置**:`src/main/java/features/PatternMatchingInstanceofExample.java` +- **启用方式**:编译/运行时需添加`--enable-preview` + +### 3. Switch Expressions(Switch表达式) +- **说明**:从预览功能转为正式功能,支持箭头语法和返回值 +- **示例位置**:`src/main/java/features/SwitchExpressionsExample.java` + +### 4. Text Blocks(文本块)- 第二次预览 +- **说明**:多行字符串文字,增强可读性和可维护性 +- **示例位置**:`src/main/java/features/TextBlocksExample.java` +- **启用方式**:编译/运行时需添加`--enable-preview` + +### 5. Helpful NullPointerExceptions(更有帮助的空指针异常) +- **说明**:改进 NullPointerException 的错误信息,更准确地指出哪个变量为 null +- **示例位置**:`src/main/java/features/NullPointerExceptionExample.java` + +### 6. Packaging Tool(打包工具)- 实验性功能 +- **说明**:提供 jpackage 工具,用于创建独立的 Java 应用程序包 +- **使用方式**:命令行工具 `jpackage`,需要单独下载 + + \ No newline at end of file diff --git a/demo2/java12-example/src/main/java/Main.java b/demo2/java14-example/src/main/java/Java14Main.java similarity index 60% rename from demo2/java12-example/src/main/java/Main.java rename to demo2/java14-example/src/main/java/Java14Main.java index bbb3c32..13c4ca5 100644 --- a/demo2/java12-example/src/main/java/Main.java +++ b/demo2/java14-example/src/main/java/Java14Main.java @@ -2,8 +2,8 @@ * @author maxiaofeng * @date 2025/7/18 10:06 */ -public class Main { +public class Java14Main { public static void main(String[] args) { - System.out.println("hello java8."); + System.out.println("hello java14."); } } diff --git a/demo2/java14-example/src/main/java/Main.java b/demo2/java14-example/src/main/java/Main.java deleted file mode 100644 index bbb3c32..0000000 --- a/demo2/java14-example/src/main/java/Main.java +++ /dev/null @@ -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."); - } -}