java9新特性

1.jdk的目录结构变化html

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

2.接口中声明私有方法java

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 APIui

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

新增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;
    }

}
相关文章
相关标签/搜索