1. 程式人生 > 實用技巧 >What's New in JDK 8 java8新特性彙總

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

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

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

一、針對java程式語言(Java Programming Language)

1.lambda表示式:一種新的語言特性,能夠把函式作為方法的引數或將程式碼作為資料。lambda表示式使你在表示函式介面(具有單個方法的介面)的例項更加緊湊。

public class Calculator {

	interface IntegerMath {
		int operation(int a, int b);
	}

	public int operateBinary(int a, int b, IntegerMath op) {
		return op.operation(a, b);
	}

	public static void main(String... args) {

		Calculator myApp = new Calculator();
		IntegerMath addition = (a, b) -> a + b;
		IntegerMath subtraction = (a, b) -> a - b;
		System.out.println("40 + 2 = " + myApp.operateBinary(40, 2, addition));
		System.out.println("20 - 10 = "
				+ myApp.operateBinary(20, 10, subtraction));
	}
}

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

myDeck.shuffle();
myDeck.sort(Comparator.comparing(Card::getRank).thenComparing(
				Comparator.comparing(Card::getSuit)));
System.out.println("Sorted by rank, then by suit "
				+ "with static and default methods");
System.out.println(myDeck.deckToString());

詳細程式碼參見《預設方法的概念與程式碼解析》http://my.oschina.net/cloudcoder/blog/215594

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

預設方法使您能夠新增新的功能到你現有庫的介面中,並確保與採用老版本介面編寫的程式碼的二進位制相容性.

4.重複註解:允許在同一宣告或型別(類,屬性,或方法)的使用中多次使用同一個註解
@Repeatable(Authorities.class) 
public @interface Authority { 
     String role(); 
} 


public @interface Authorities { 
    Authority[] value(); 
} 


public class RepeatAnnotationUseNewVersion { 
    @Authority(role="Admin") 
    @Authority(role="Manager") 
    public void doSomeThing(){ } 
}

5.型別註解:在java 8之前,註解只能是在宣告的地方所使用,比如類,方法,屬性;java 8裡面,註解可以應用在任何地方。

型別註解被用來支援在Java的程式中做強型別檢查。配合外掛式的check framework,可以在編譯的時候檢測出runtime error,以提高程式碼質量

6.改善了型別推斷

private List<Card> entireDeck= new ArrayList<>();

7.方法引數反射

二、針對集合(Collections)
1.提供了新包java.util.stream,這個包提供了Stream API功能,支援以函式風格(functional-style)去處理流中的元素。在Collections API中已經整合了Stream API,可以在集合上進行批量操作(bulk operations),如順序或並行的map-reduce轉換。
Stream API提供了一種操作大資料的介面,讓資料操作更容易和更快。它具有過濾、對映以及減少遍歷數等方法,這些方法分兩種:中間方法和終端方法,“流”抽象天生就該是持續的,中間方法永遠返回的是Stream,因此如果我們要獲取最終結果的話,必須使用終點操作才能收集流產生的最終結果。

Stream API的目的是利用多核技術可將大資料通過多核並行處理,提高資料的處理效率

public static void testInt(Integer... numbers) {
		List<Integer> l = Arrays.asList(numbers);
		List<Integer> r = l.stream()
				.map(e -> new Integer(e))
				.filter(e -> e > 2)
				.distinct()
				.collect(Collectors.toList());
		System.out.println("testInt result is: " + r);
	}

呼叫: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的模組化專案做準備。

詳見:http://docs.oracle.com/javase/8/docs/technotes/guides/compactprofiles/compactprofiles.html

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 Security1 JMX
XML JAXP2 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的編譯後,佔用的大致空間見下圖:

四、安全性

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

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

五、JavaFX

1.3D Graphics包括3D shapes, camera, lights, subscene, material, picking, and antialiasing等

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

其他可以參見原文

六 工具(tools)
  1. 增加了jss命令,用於呼叫 Nashorn engine
  2. java命令可以啟動JavaFX applications
  3. jdeps命令列工具用於分析類檔案
  4. 針對javac tool增加了一些引數和功能
  5. 針對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

程式碼可以參考《預設方法的概念與程式碼解析》http://my.oschina.net/cloudcoder/blog/215594

十、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很相似,如下圖所示

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

新引數(MaxMetaspaceSize)用於限制本地記憶體分配給類元資料的大小。如果沒有指定這個引數,元空間會在執行時根據需要動態調整。

3:Default Methods in the Java Programming Language are supported by the byte code instructions for method invocation.

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

十七、其他改進

詳細參見原文

原文參見

http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html

來源:站長資訊