天天看點

What's New in JDK 8 java8新特性彙總

    oracle甲骨文公司終于在2014年3月釋出了java 8正式版,它是java的一個裡程牌版本,帶來了諸多新特性。 

    針對這些新特性彙總如下:

一、針對java程式設計語言(java programming language)

    1.lambda表達式:一種新的語言特性,能夠把函數作為方法的參數或将代碼作為資料。lambda表達式使你在表示函數接口(具有單個方法的接口)的執行個體更加緊湊。

    2.方法引用 是lambda表達式的一個簡化寫法,所引用的方法其實是lambda表達式的方法體實作,這樣使代碼更容易閱讀  

    3.預設方法:java 8引入default method,或者叫virtual extension method,目的是為了讓接口可以事後添加新方法而無需強迫所有實作該接口的類都提供新方法的實作。也就是說它的主要使用場景可能會涉及代碼演進。

    預設方法使您能夠添加新的功能到你現有庫的接口中,并確定與采用老版本接口編寫的代碼的二進制相容性.

      4.重複注解:允許在同一聲明或類型(類,屬性,或方法)的使用中多次使用同一個注解 

    5.類型注解:在java 8之前,注解隻能是在聲明的地方所使用,比如類,方法,屬性;java 8裡面,注解可以應用在任何地方。

    類型注解被用來支援在java的程式中做強類型檢查。配合插件式的check framework,可以在編譯的時候檢測出runtime error,以提高代碼品質

    6.改善了類型推斷

     7.方法參數反射

二、針對集合(collections) 

      1.提供了新包java.util.stream,這個包提供了stream api功能,支援以函數風格(functional-style)去處理流中的元素。在collections api中已經整合了stream api,可以在集合上進行批量操作(bulk operations),如順序或并行的map-reduce轉換。 

    stream api提供了一種操作大資料的接口,讓資料操作更容易和更快。它具有過濾、映射以及減少周遊數等方法,這些方法分兩種:中間方法和終端方法,“流”抽象天生就該是持續的,中間方法永遠傳回的是stream,是以如果我們要擷取最終結果的話,必須使用終點操作才能收集流産生的最終結果。 

    stream api的目的是利用多核技術可将大資料通過多核并行處理,提高資料的處理效率  

    調用:testint(2, 3, 4, 2, 3, 5, 1);

    2.針對有key collisions的hashmaps的性能改進

三、compact profiles 

     1:compact profiles包含 java se平台預定義子集,使應用程式員不需要整個jre平台即可部署和運作在小型裝置上,開發人員可以基于目标硬體的可用資源選擇一個合适的jre運作環境。 

    目前提供了三種compact profiles,分别是compact1、compact2、compact3,他們的關系是compact1<compact2<compact3。每個compact profiles包括低版本的profiles(compact2 is a superset of compact1 即compact2是compact1的超集,the full se api is a superset of the compact3 profiles 而the full se api又是compact3的超集)

    在指令javac,jdeps指令中都增加了-profile參數

    該特性也是為java9的子產品化項目做準備。

full se api

beans

jni

jax-ws

preferences

accessibility

idl

rmi-iiop

corba

print service

sound

swing

java 2d

awt

drag and drop

input methods

image i/o

compact3

jmx

management

instrumentation

compact2

jdbc

rmi

xml jaxp

compact1

core (java.lang.*)

security

serialization

networking

ref objects

regular expressions

date and time

input/output

collections

logging

concurrency

reflection

jar

zip

versioning

internationalization

jndi

override mechanism

extension mechanism

scripting

使用不同的compact profiles的編譯後,占用的大緻空間見下圖:

What's New in JDK 8 java8新特性彙總

四、安全性

    這部分的内容較多,主要如下: 

    主要是一些加密算法的支援,詳細可以參見原文

五、javafx 

    1.3d graphics包括3d shapes, camera, lights, subscene, material, picking, and antialiasing等

    2.webview也提供了新的特性和功能改進

     其他可以參見原文

六 工具(tools) 

增加了jss指令,用于調用 nashorn engine 

java指令可以啟動javafx applications 

jdeps指令行工具用于分析類檔案 

針對javac tool增加了一些參數和功能 

針對javadoc tool增加了新的功能,如doctree 

七、國際化 

    1.針對unicode的增強,包括支援unicode6.2.0 

    2.adoption of unicode cldr data and the java.locale.providers system property 

    3.new calendar and locale apis 

    4.ability to install a custom resource bundle as an extension 

八、部署

有兩項,都是針對權限方面的限制 

九、date-time package  

    提供了一個完整的data-time model的包java.time 

十、scripting

    nashorn javascript engine

十一、java.lang and java.util packages 

    1. parallel array sorting   并發數組排序,增加針對資料排序的處理效率 

    2. standard encoding and decoding base64 

    3. unsigned arithmetic support  針對無符号運算的支援 

十二、jdbc 

    1. the jdbc-odbc bridge has been removed.  jdbc-odbc已經被删除 

    2. jdbc 4.2 introduces new features. 

十三、java db 

    jdk 8 includes java db 10.10. 

十四、 networking 

    the class java.net.urlpermission has been added. 

    in the class java.net.httpurlconnection, if a security manager is installed, calls that request to open a connection require permission. 

十五、concurrency 

    1. classes and interfaces have been added to the java.util.concurrent package. 

    2. methods have been added to the java.util.concurrent.concurrenthashmap class to support aggregate operations based on the newly added streams facility and lambda expressions. 

    3. classes have been added to the java.util.concurrent.atomic package to support scalable updatable variables. 

    4. methods have been added to the java.util.concurrent.forkjoinpool class to support a common pool. 

    5. the java.util.concurrent.locks.stampedlock class has been added to provide a capability-based lock with three modes for controlling read/write access.

十六、hotspot

    1. hardware intrinsics were added to use advanced encryption standard (aes). the useaes and useaesintrinsics flags are available to enable the hardware-based aes intrinsics for intel hardware. the hardware must be 2010 or newer westmere hardware. for example, to enable hardware aes, use the following flags:

-xx:+useaes -xx:+useaesintrinsics

to disable hardware aes use the following flags:

-xx:-useaes -xx:-useaesintrinsics

      支援基于硬體特性的aes,但硬體需要是2010年以後的裝置或更新的westmere硬體

    2: removal of permgen.

        移除了permgen(記憶體的永久儲存區域)

        jdk8 hotspot jvm 将移除永久區,使用本地記憶體來存儲類中繼資料資訊并稱之為:元空間(metaspace);這與oracle jrockit 和ibm jvm’s很相似,如下圖所示

What's New in JDK 8 java8新特性彙總

    預設情況下,類中繼資料隻受可用的本地記憶體限制(容量取決于是32位或是64位作業系統的可用虛拟記憶體大小)。

    新參數(maxmetaspacesize)用于限制本地記憶體配置設定給類中繼資料的大小。如果沒有指定這個參數,元空間會在運作時根據需要動态調整。

    3:default methods in the java programming language are supported by the byte code instructions for method invocation.

    支援java程式設計語言中的預設方法

十七、其他改進 

    詳細參見原文

原文參見

<a href="http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html">http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html</a>