What's New in JDK 8 java8新特性汇总

时间:2020-08-08
本文章向大家介绍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

 
来源:站长资讯

原文地址:https://www.cnblogs.com/aabbc6/p/13460631.html