[update] java12-example 示例完成
java13-example 示例完成
This commit is contained in:
parent
2830ed16e8
commit
59a4adc568
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>
|
<configuration>
|
||||||
<source>${maven.compiler.source}</source>
|
<source>${maven.compiler.source}</source>
|
||||||
<target>${maven.compiler.target}</target>
|
<target>${maven.compiler.target}</target>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>--enable-preview</arg>
|
||||||
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</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());
|
||||||
|
}
|
||||||
|
}
|
26
demo2/java13-example/README.md
Normal file
26
demo2/java13-example/README.md
Normal file
@ -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`参数使用
|
@ -24,6 +24,17 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<source>${maven.compiler.source}</source>
|
<source>${maven.compiler.source}</source>
|
||||||
<target>${maven.compiler.target}</target>
|
<target>${maven.compiler.target}</target>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>--enable-preview</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.0.0-M7</version>
|
||||||
|
<configuration>
|
||||||
|
<argLine>--enable-preview</argLine>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
27
demo2/java13-example/src/main/java/Java13Main.java
Normal file
27
demo2/java13-example/src/main/java/Java13Main.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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 = """
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<p>Hello, Java 13!</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""";
|
||||||
|
|
||||||
|
System.out.println("\nHTML示例:");
|
||||||
|
System.out.println(html);
|
||||||
|
}
|
||||||
|
}
|
32
demo2/java13-example/src/main/java/features/ZGCExample.java
Normal file
32
demo2/java13-example/src/main/java/features/ZGCExample.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
35
demo2/java14-example/README.md
Normal file
35
demo2/java14-example/README.md
Normal file
@ -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`,需要单独下载
|
||||||
|
|
||||||
|
</markdown>
|
@ -2,8 +2,8 @@
|
|||||||
* @author maxiaofeng
|
* @author maxiaofeng
|
||||||
* @date 2025/7/18 10:06
|
* @date 2025/7/18 10:06
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Java14Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("hello java8.");
|
System.out.println("hello java14.");
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user