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

}
相關文章
相關標籤/搜索