目录
1 模块化
- jdk9增加模块化机制
- 在模块提供方通过exports指令将需要提供给三方使用的包对外暴漏
- 在模块使用方通过requires指令将需要依赖的模块导入
- jdk之前的rt.jar就被拆分为了很多模块
2 jshell
- jdk9新增jshell工具
- jshell提供了一个交互式命令界面
- 无需编译就可以直接执行代码

3 接口私有方法
- 方便接口中公共并且不想暴漏给子类的方法提取
public interface HelloService {
default void test1(){
System.out.println("test1");
common();
}
default void test2(){
System.out.println("test2");
common();
}
private void common(){
System.out.println("common");
}
}
4 改进 try-whith-resources
- 允许在资源在try()外定义,然后只要在括号内指明需要自动关闭的资源变量名即可
public class App {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\\jwork\\jdk9-demo\\module1\\src\\main\\java\\module-info.java");
try (fis){
byte[] buf = new byte[1024];
int length = 0;
while ((length=fis.read(buf))!=-1){
System.out.println(new String(buf,0,length));
}
}
}
}
5 改进砖石操作符
- 增强了类型推断机制
- 书写的匿名内部类具有泛型时,只需要声明变量时指定泛型类型即可

6 限制使用单独下划线标识符
7 String存储结构变更
- string中存储字符串值的Char数组替换成了byte数组
- 增加编码标识coder

8 快速创建只读集合
- 以下方式创建的集合只读
- 新增或者修改会抛异常
public class App {
public static void main(String[] args) throws IOException {
List<String> hello = List.of("hello", "World");
Set<String> hello1 = Set.of("hello", "World");
Map<Integer, String> integerStringMap = Map.of(1, "hello", 2, "World");
}
}
9 增强Stream API
- 新增方法dropWhile(),遇到第一个不符合条件的则终止操作
- 新增方法takeWhile(),遇到第一个不符合条件的则终止操作
- 新增方法ofNullable(),允许在Stream中放入null
public class App {
public static void main(String[] args) throws IOException {
List<String> hello = List.of("小青", "画画类","大神");
hello.stream().dropWhile(s -> s.length() == 2).forEach(System.out::println);
System.out.println("============================");
hello.stream().takeWhile(s -> s.length() == 2).forEach(System.out::println);
System.out.println("============================");
Stream.ofNullable(null).forEach(System.out::println);
}
}
10 改进Optional类
- 新增方法stream()
- 新增方法ifPersentOrElse()
- 新增方法or()
public class App {
public static void main(String[] args) throws IOException {
List<String> hello = List.of("小青", "画画类", "大神");
Optional.of(hello).stream().forEach(System.out::println);
System.out.println("============================");
Optional<String> hello1 = Optional.ofNullable("hello");
hello1.ifPresentOrElse(System.out::println, () -> {
System.out.println("空空如也");
});
System.out.println("============================");
Optional<String> hello2 = Optional.ofNullable(null);
hello2.ifPresentOrElse(System.out::println, () -> {
System.out.println("空空如也");
});
System.out.println("============================");
Optional<String> hello3 = Optional.ofNullable("hello").or(() -> {
return Optional.of("备用hello");
});
System.out.println(hello3.get());
System.out.println("============================");
Optional<Object> hello4 = Optional.ofNullable(null).or(() -> {
return Optional.of("备用hello");
});
System.out.println(hello4.get());
}
}
11 多分辨率图像APi
12 全新HTTP客户端API
- 支持HTTP2
- 自持websocket
public class App {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient.Builder builder = HttpClient.newBuilder();
HttpClient client = builder.version(HttpClient.Version.HTTP_2)
.executor(Executors.newFixedThreadPool(5))
.followRedirects(HttpClient.Redirect.NEVER)
.build();
URI uri = URI.create("http://www.baidu.com");
HttpRequest request = HttpRequest.newBuilder(uri).version(HttpClient.Version.HTTP_2).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
System.out.println(response.body());
}
}
13 智能JAVA编译工具
- 智能java编译工具sjavac第一阶段始于JEP139项目,用于在多核处理器情况下提升JDK编译速度
- 如今已经进入第二阶段,即JEP199,其目的是改进Java编译工具,并取代javac
- jdk9更新了javac编译器以便java9代码运行在低版本java中
14 统一JVM日志系统
15 java动态编译
- JIT(Just-In-time)编译器可以在运行时将热点代码编译成本地代码,速度很快
- 但是JIT需要花费较长时间才能热身完成
- 并且未优化的java代码仍旧会运行很慢
- AoT编译就是为了解决上述问题而生
- 在JDK9中AOT(JEP295:Ahead-of-Time Compilation)作为实验特性被引入,开发者可以利用jaotc工具将重点代码转换成类库一样的文件
- 使得Java应用再被虚拟机启动前编译为原生代码,旨在改进小型和大型应用程序的启动时间,同时对峰值性能影响很小
760

被折叠的 条评论
为什么被折叠?



