Java 6-7 小版本更新
2026/1/15大约 3 分钟JavaJava版本特性JavaJava新特性LambdaStreamOptional模块化虚拟线程
Java 6 (2006年12月)
Java 6 主要是性能优化和 API 增强,没有引入新的语言特性。
主要更新
1. 脚本引擎 (JSR 223)
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// 执行 JavaScript 代码
engine.eval("print('Hello from JavaScript')");
// 传递变量
engine.put("name", "Java");
engine.eval("print('Hello, ' + name)");2. 编译器 API (JSR 199)
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, "MyClass.java");3. JDBC 4.0
// 自动加载驱动,无需 Class.forName()
Connection conn = DriverManager.getConnection(url, user, password);
// 增强的异常处理
try {
// 数据库操作
} catch (SQLException e) {
for (Throwable t : e) {
System.out.println(t.getMessage());
}
}4. 其他改进
- JVM 性能优化 - 同步优化、垃圾回收改进
- Web Services - JAX-WS 2.0 支持
- JAXB 2.0 - XML 绑定增强
- StAX - 流式 XML 解析
- Console 类 - 控制台输入输出
Console console = System.console();
if (console != null) {
String username = console.readLine("Username: ");
char[] password = console.readPassword("Password: ");
}Java 7 (2011年7月)
Java 7 引入了多项语法糖和 API 改进,代号 "Dolphin"。
1. 钻石操作符 (Diamond Operator)
// Java 7 之前
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
// Java 7 之后
Map<String, List<Integer>> map = new HashMap<>();2. try-with-resources
自动资源管理,实现 AutoCloseable 接口的资源会自动关闭。
// Java 7 之前
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("file.txt"));
String line = br.readLine();
} finally {
if (br != null) {
br.close();
}
}
// Java 7 之后
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line = br.readLine();
} // 自动关闭
// 多个资源
try (FileInputStream fis = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt")) {
// 使用资源
}3. switch 支持 String
String day = "Monday";
switch (day) {
case "Monday":
System.out.println("周一");
break;
case "Tuesday":
System.out.println("周二");
break;
default:
System.out.println("其他");
}4. 多异常捕获
// Java 7 之前
try {
// 代码
} catch (IOException e) {
handleException(e);
} catch (SQLException e) {
handleException(e);
}
// Java 7 之后
try {
// 代码
} catch (IOException | SQLException e) {
handleException(e);
}5. 数字字面量改进
// 二进制字面量
int binary = 0b1010; // 10
// 数字下划线(提高可读性)
int million = 1_000_000;
long creditCard = 1234_5678_9012_3456L;
double pi = 3.14_15_92_65;6. NIO.2 (JSR 203)
全新的文件系统 API。
// Path API
Path path = Paths.get("/home/user/file.txt");
Path fileName = path.getFileName();
Path parent = path.getParent();
// Files 工具类
// 读取文件
List<String> lines = Files.readAllLines(path);
byte[] bytes = Files.readAllBytes(path);
// 写入文件
Files.write(path, "Hello".getBytes());
Files.write(path, lines);
// 复制、移动、删除
Files.copy(source, target);
Files.move(source, target);
Files.delete(path);
// 遍历目录
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry : stream) {
System.out.println(entry.getFileName());
}
}
// 文件属性
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Size: " + attrs.size());
System.out.println("Created: " + attrs.creationTime());7. Fork/Join 框架
用于并行计算的框架。
public class SumTask extends RecursiveTask<Long> {
private final long[] array;
private final int start, end;
private static final int THRESHOLD = 1000;
public SumTask(long[] array, int start, int end) {
this.array = array;
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if (end - start <= THRESHOLD) {
// 直接计算
long sum = 0;
for (int i = start; i < end; i++) {
sum += array[i];
}
return sum;
} else {
// 分治
int mid = (start + end) / 2;
SumTask left = new SumTask(array, start, mid);
SumTask right = new SumTask(array, mid, end);
left.fork();
return right.compute() + left.join();
}
}
}
// 使用
ForkJoinPool pool = new ForkJoinPool();
long result = pool.invoke(new SumTask(array, 0, array.length));总结
| 版本 | 主要特性 |
|---|---|
| Java 6 | 脚本引擎、编译器API、JDBC 4.0、性能优化 |
| Java 7 | 钻石操作符、try-with-resources、switch String、NIO.2、Fork/Join |
Java 6 和 Java 7 虽然没有 Java 5 那样革命性的变化,但提供了许多实用的语法糖和 API 改进,为后续版本的发展奠定了基础。
