java9新特性

1.jdk的目录结构变化

jdk9的安装文件夹中不包含jre文件夹,eclispe在低的版本中无法加载jdk9

2.接口中声明私有方法

public interface InterfaceTest {
    //java9新特性,可以在接口中添加私有的方法
    private void test() {
        System.out.println("InterfaceTest.test");
    }
}

3.钻石符能够与匿名内部类一起使用

public class DeamonInnerTest {
    //java9中才能这么使用,在java8中钻石符不能与匿名内部类一起使用
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(){
        };
    }
}

4.java8在try的小括号里面声明的资源会自动的关闭,所以不用写finally,但是不能用括号外的实例化对象,但是java9可以,但是不可再次赋值,被默认的加上final

public class TryTest {
    public static void main(String[] args) throws FileNotFoundException {
        //java8中能这么使用
        try( FileInputStream   fileInputStream= new  FileInputStream("ss")){
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //java8不能按如下使用     java9中才可以这可使用
        FileInputStream fileInputStream = new FileInputStream("ss");
        try( fileInputStream){
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //java9中不能这么使用
        FileInputStream fileInputStream1 = null;
        try( fileInputStream1= new FileInputStream("ss")){
            fileInputStream1.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5._下划线不能单独使用

public class _Test {
    public static void main(String[] args) {
        //java9 中不能这么使用  java8中可以
         String  _=null;
    }
}

6.String的底层存储不再是字符数组,改成了字节数组,StringBuffer,StringBuilder也是做了相应的改变,减少内存的占用

 //    private final byte[] value;   java9源码
 //  private final char value[];  java8源码   

7.集合的只读只用List.of,Set.of等方法就可以了,较以前的方法更为简单

public class OnlyReadTest {
    public static void main(String[] args) {
        //java9新增的创建只读的集合
        List<Integer> integers = List.of(1, 2, 3, 4, 5);
        //java8中设置只读集合如下
        List<Integer> integers1 = Arrays.asList(1, 2, 3, 4, 5);
        Collections.unmodifiableList(integers1);
        //其他类似Set,Map等
    }
}    

8.增强Stream API

新增takeWhile方法是遇到不合适的就不跑了,取之前的元素,而filter是一致跑到末尾,

新增dropWhile与takewhile相反取的是后续的元素,遇到不合适的就不跑了,取之后的元素,包括自身

新增ofNullable方法是允许所以的元素为null的,而在java8中需要不全为null

在迭代无线流上新增了一个可以断言的重载方法

public class StreamTest {
    public static void main(String[] args) {
        //java9新增的takeWhile方法  1  4   8
        Stream.of(1,4,8,9,11,5,6).takeWhile((x)->x<9).forEach(System.out::print);
        //java9新增的dropWhile方法  9  11  5  6
        Stream.of(1,4,8,9,11,5,6).dropWhile((x)->x<9).forEach(System.out::print);
        //java8中的stream不能全部为null,会报空指针异常  java.lang.NullPointerException
        // Stream.of(null).forEach(System.out::println);
        //java9新增方法是的不会出现空指针异常
        Stream.ofNullable(null).forEach(System.out::println);
        //java9在递归流中新增重载方法
        Stream.iterate(0,(x)->x>10,(x)->x+1);
        //java8中只能这么使用
        Stream.iterate(0,(x)->x+1).limit(5);
    }
}

9.在optional中添加了可以获取Stream的方法

public class OptinalStreamTest {
    public static void main(String[] args) {
        //4 5
        List<Integer> integers = Arrays.asList(1, 4, 5);
        Optional.of(integers).stream().flatMap((x)->x.stream()).filter((x)->x>3).forEach(System.out::print);
    }
}

10.提供了一个新的Api,HttpClient,替代仅适用于blocking模式的HttpURLConnection (HttpURLConnection是在HTTP 1.0的时代创建的,并使用了协议无关的方法),并提供对WebSocket 和 HTTP/2的支持。(HTTP/1.1和HTTP/2的主要区别是如何在客户端和服务器之间构建和传输数据。HTTP/1.1依赖于请求/响应周期。 HTTP/2允许服务器“push”数据:它可以发送比客户端请求更多的数据。 这使得它可以优先处理并发送对于首先加载网页至关重要的数据。)

public class HttpClientTest {
    public static void main(String[] args) throws IOException, InterruptedException {
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest build = HttpRequest.newBuilder(URI.create("https://www.baidu.com/")).GET().build();
        HttpResponse<String> send = httpClient.send(build, HttpResponse.BodyHandler.asString());
        System.out.println(send.statusCode());
        System.out.println(send.version().name());
        System.out.println(send.body());
    }
}
//src 右键 新建module-info.java
module modeltest {
    requires jdk.incubator.httpclient;
}
 

11.新的API定义在java.awt.image包下,将不同分辨率的图像封装到一张(多分辨率的)图像中,作为它的变体,基于当前屏幕分辨率大小和运用的图像转换算法,
java.awt.Graphics类可以从接口MultiResolutionImage获取所需的变体。

12.以前的版本是支持html4现在支持html5

13.模块化module

module muduletest1 {
    //导入模块
    requires   muduletest2;
}
package cn.muduletest1;
import cn.muduletest2.Person;
public class ModuleTest {
    public static void main(String[] args) {
        Person person = new Person();
    }
}
module muduletest2 {
    //导出包
    exports  cn.muduletest2;
}
package cn.muduletest2;

public class Person {
    private   String  name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
原文地址:https://www.cnblogs.com/gg128/p/9427965.html