`

转载:Java 7新特性引领Java8

    博客分类:
  • Java
阅读更多

原文:Java 7 Features Which Enable Java 8

It's a truism of the tech industry that developers are never happier than when there's free beer or an opportunity to complain about something on offer.

So despite the efforts of Mark Reinhold and the Java team to involve the community in the roadmap after the Oracle acquisition (the Plan A / Plan B decision), many Java developers feel that Java 7 was "not much of a release".

In this article, I'll try to refute this thesis, by exploring the features in Java 7 which lay the groundwork for the new features in Java 8.

 

 

Java has often been criticised for being overly verbose. One of the most common areas where this complaint is expressed is in assignment. In Java 6, we are forced to write assignment statements like this:

Map<String, String> m = new HashMap<String, String>();

This statement contains a lot of redundant information - we should be able to somehow have the compiler figure out more of this by itself, and not require the programmer to be quite so explicit.

 

In fact, languages like Scala do a large amount of type inference from expressions, and in fact assignment statements can be written as simply as this:

val m = Map("x" -> 24, "y" -> 25, "z" -> 26);

The keyword val indicates that this variable may not be reassigned to (like the keyword final for Java variables). No type information is specified about the variable at all - instead the Scala compiler examines the right-hand side of the assignment and determines the correct type for the variable by looking at which value is being assigned.

In Java 7, some limited type inference capabilities were introduced, and assignment statements can now be written like this:

Map<String, String> m = new HashMap<>();

The key differences between this and the Scala form is that in Scala, values have explicit types, and it is the type of variables that is inferred. In Java 7, the type of variables is explicit, and type information about values is what is inferred.

Some developers have complained that they would have preferred the Scala solution, but it turns out to be less convenient in the context of a major feature for Java 8 - lambda expressions.

In Java 8, we can write a function which adds 2 to an integer like this:

Function<Integer, Integer> fn = x -> x + 2;

The interface Function is new with Java 8 - it resides in the package java.util.function, along with specialized forms for primitive types. However, we've chosen this syntax as it is very similar to the Scala equivalent and allows the developer to see the similarities more easily.

By explicitly specifying the type of fn as a Function which takes one Integer argument and returns another Integer, then the Java compiler is able to infer the type of the parameter x - Integer. This is the same pattern that we saw in Java 7 diamond syntax - we specify the types of variables, and infer the type of values.

Let's look at the corresponding Scala lambda expression:

val fn = (x : Int) => x + 2;

Here, we have to explicitly specify the type of the parameter x, as we don't have the precise type of fn, and so we have nothing to infer from. The Scala form is not hugely difficult to read, but the Java 8 form has a certain cleanliness of syntax which can be directly traced back to the diamond syntax of Java 7.

 

  • Method Handles

Method Handles are simultaneously the most important new feature of Java 7, and the one which is least likely to feature in the day-to-day life of most Java developers.

A method handle is a typed reference to a method for execution. They can be thought of as "typesafe function pointers" (for developers familiar with C/C++) or as "Core Reflection reimagined for the modern Java developer".

Method handles play a huge part in the implementation of lambda expressions. Early prototypes of Java 8 had each lambda expression converted to an anonymous inner class at compile time.

More recent betas are more sophisticated. Let's start by recalling that a lambda expression (at least in Java) comprises a function signature (which in the method handles API will be represented by a MethodType object) and a body, but not necessarily a function name.

This suggests that we could convert the lambda expression into a synthetic method which has the correct signature and which contains the body of the lambda. For example, our example:

Function<Integer, Integer> fn = x -> x + 2;

is turned by the Java 8 compiler into a private method with this bytecode:

 private static java.lang.Integer lambda$0(java.lang.Integer);
   descriptor: (Ljava/lang/Integer;)Ljava/lang/Integer;
   flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
   Code:
    stack=2, locals=1, args_size=1
      0: aload_0
      1: invokevirtual #13 // Method java/lang/Integer.intValue:()I
      4: iconst_2
      5: iadd
      6: invokestatic #8 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      9: areturn

This has the correct signature (takes in an Integer, and returns another one), and semantics. To use this lambda expression, we will take a method handle which refers to it and use it to build an object of the appropriate type, as we'll see in the next feature we discuss.

 

  • invokedynamic

The final feature of Java 7 that opens the door for Java 8 is even more esoteric than method handles. This is the new bytecode invokedynamic - the first new bytecode to be added to the platform since Java 1.0. This feature is almost impossible for Java developers to make use of in version 7, because version 7 javac will not, under any circumstances, emit a classfile which contains it.

Instead, the bytecode was designed for use by developers of non-Java languages, such as JRuby, which require much more dynamic dispatch than Java. To see how invokedynamic works, let's discuss how Java's method calls are compiled into bytecode.

A standard Java method call will be turned into a piece of JVM bytecodes which is often referred to as a call site. This comprises a dispatch opcode (such as invokevirtual, for regular instance method calls) and a constant (an offset into the Constant Pool of the class) which indicates which method is to be called.

The different dispatch opcodes have different rules that govern how method lookup is done, but until Java 7 the constant was always a straightforward indication of which method was to be called.

invokedynamic is different. Instead of providing a constant which directly indicates which method is to be called, invokedynamic instead provides an indirection mechanism that allows user code to decide which method to call at runtime.

When an invokedynamic site is first encountered, it does not have a known target yet. Instead, a method handle (called a bootstrap method) is invoked. This bootstrap method returns a CallSite object, which contains another method handle, which is the actual target of the invokedynamic call.

1) invokedynamic site encountered in the execution stream (initially unlinked) 2) Call bootstrap method and return a CallSite object 3) CallSite object contains a method handle (the target) 4) Invoke the target method handle

The bootstrap method is the way in which user code chooses which method needs to be called. For lambda expressions, the platform uses a library-supplied bootstrap method called a lambda meta-factory.

This has static arguments which contain a method handle to the synthesized method (see last section), and the correct signature for the lambda.

The meta-factory returns a CallSite that contains a method handle which will in turn return an instance of the correct type that the lambda expression has been converted to. So a statement like:

Function<Integer, Integer> fn = x -> x + 2;

is converted to an invokedynamic call like this:

Code:
  stack=4, locals=2, args_size=1
     0: invokedynamic #2, 0 // InvokeDynamic #0:apply:()Ljava/util/function/Function;
     5: astore_1

The invokedynamic bootstrap method is the static method LambdaMetafactory.metafactory(), which returns a CallSite object which is linked to a target method handle, which will return an o bject which implements the Function interface.

When the invokedynamic instruction is complete, an object which implements Function and which has the lambda expression as the contents of its apply() method is sat on top of the stack, and the rest of the code can proceed normally.

  • Conclusion

Getting lambda expressions into the Java platform was always going to be a challenging endeavour, but by ensuring that the proper groundwork was in place, Java 7 eased that effort considerably. Plan B not only provided developers with the early release of Java 7 but also allowed core technologies to be made fully road-tested before their use in Java 8 and especially in lambda expressions.

About the Author

Ben Evans is the CEO of jClarity, a startup which delivers performance tools to help development & ops teams. He is an organizer for the LJC (London JUG) and a member of the JCP Executive Committee, helping define standards for the Java ecosystem. He is a Java Champion; JavaOne Rockstar; co-author of “The Well-Grounded Java Developer” and a regular public speaker on the Java platform, performance, concurrency, and related topics.

分享到:
评论

相关推荐

    java错误处理:java.lang.OutOfMemoryError: Java heap space

    ### Java 错误处理:java.lang.OutOfMemoryError: Java heap space 在Java应用程序开发过程中,经常遇到的一个问题就是内存溢出错误,特别是在处理大量数据或长时间运行的应用时。其中,“java.lang....

    全网详解(波哥)Java8新特性(Lambda、Stream、LocalDate等)新特性

    全网详解(波哥)Java8新特性(Lambda、Stream、LocalDate等)新特性 自学java的同行们应该都要学习java8的新特性,譬如:(Lambda、Stream、LocalDate等)!本人在学习java的时候看的是波哥的视频,确实讲的不错,很...

    Java8视频新特性 百度云

    Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性Java8新特性

    尚硅谷Java8新特性下载

    根据提供的文件信息,我们可以深入探讨Java 8的新特性。这些特性极大地提高了Java语言的功能性和效率,使得开发人员能够编写更加简洁、高效的代码。 ### Java 8 新特性概述 #### Lambda 表达式 Lambda表达式是Java...

    Java8新特性视频 最新的讲解视频

    以下是对Java8新特性的详细解读: 1. **函数式编程**:Java8引入了Lambda表达式,这是一种简洁的匿名函数写法,使得处理集合数据更加方便。Lambda表达式可以作为参数传递,也可以作为方法的返回值。同时,函数接口...

    Java9新特性

    本套《Java9新特性》视频涵盖oracle公司2017年9月公布的java 9 新特性的核心内容。 主要包含:模块化系统,REPL工具:jshell命令,多版本兼容jar包,语法的新变化:接口私有方法、异常处理、钻石操作符、String存储...

    《Java 8 in Action》是一本关于 Java 编程语言的书籍,重点介绍了 Java 8 中引入的新特性和改进

    该书由 Mario Fusco、Alan Mycroft 和 Raoul-Gabriel Urma 合著,旨在帮助读者深入了解 Java 8,并掌握其中的关键...其他新特性: 简要介绍 Java 8 中引入的其他新特性,如接口的默认方法、方法引用、Optional 类型等。

    java8特性资源合集(电子档)

    1.Java8:Lambda序列化? 2.Java 8 lambda 最佳实践 3.Java8 lambda表达式10个示例 ...... 30.在Java 8下更好地利用枚举 31.在 Java 8 中避免 Null 检查 32.鲜为人知的Java8特性:泛化目标类型推断

    java7 32位版

    Java 7,又称为Java SE 7 (Java Standard Edition 7),是Java的重要里程碑,它在2011年发布,带来了许多新特性和性能优化。以下是一些关键的Java 7特性: 1. **多语言支持**:Java 7引入了新的`try-with-resources`...

    JAVA.2核心技术.卷II:高级特性(原书第7版).part2.rar

    本书对Java技术的阐述精确到位,叙述方式深入浅出,并包含大量示例,从而帮助读者充分理解Java语言以及Java类库的相关特性。  本书适合软件开发人员、高等院校教师和学生参考。 本资源共分5部分,分别为: JAVA....

    Java8新特性之JavaFX 8_动力节点Java学院

    Java8新特性之JavaFX 8_动力节点Java学院,动力节点口口相传的Java黄埔军校

    javaJDK8javaJDK8

    这个版本是Oracle公司于2014年3月18日正式发布的,对Java社区有着深远的影响,因为它是Java 8的主要发行版,引入了许多重要的新特性和改进。 1. **lambda表达式**:Java 8最重要的特性之一就是引入了lambda表达式,...

    java1.5新特性

    java1.5之后的新特性: 1,枚举(enum) 作用:一般用于代表一组相同类型的常用常量。 原理:语法结构与java类的语法不一样,但是经过编译器编译之后产生的是一个class文件。该class文件经过反编译之后实际上是...

    java源码包---java 源码 大量 实例

    内容索引:JAVA源码,综合应用,J2me游戏,PNG,图形处理  这是个J2ME控制台程序,它能剔除PNG文件中的非关键数据段,减少文件大小从而达到压缩图片的目的。而图片的质量并不会受到损失。使用时候只需在控制台窗口执行...

    Java8 64位免安装版

    JDK8是Oracle公司发布的Java平台标准版(Java SE)的一个重要版本,引入了许多新特性,对开发者的工作效率和代码质量产生了显著提升。 1. **Lambda表达式**:Java 8最重要的特性之一就是引入了Lambda表达式,这是一...

    JAVA.2核心技术.卷II:高级特性(原书第7版).part3.rar

    本书对Java技术的阐述精确到位,叙述方式深入浅出,并包含大量示例,从而帮助读者充分理解Java语言以及Java类库的相关特性。  本书适合软件开发人员、高等院校教师和学生参考。 本资源共分5部分,分别为: JAVA....

    《Java7并发编程实战手册》书中实例代码

    4. **try-with-resources语句**:这是Java 7的一个新特性,它可以自动关闭在try语句块中打开的资源,对于使用`Closeable`接口的并发操作尤其有用,如数据库连接、网络连接和文件流等。 5. **线程局部变量改进**:`...

    JAVA8API-官方文档下载-中文版

    这份中文版的官方文档使得国内开发者能够更加方便地理解和使用Java 8的新特性,提升开发效率。 **核心语言特性** 1. **lambda表达式**: Java 8引入了函数式编程的概念,通过lambda表达式可以简洁地表示匿名函数,...

    JDK18-java-se-language-updates.pdf

    1. 提高开发效率:Java 语言更新的新特性和改进使得开发者可以更加快速和高效地开发 Java 应用程序。 2. 提高应用性能:Java 语言更新的新特性和改进使得 Java 应用程序的性能得到了显著的改善。 3. 提高应用安全:...

    JAVA.2核心技术.卷II:高级特性(原书第7版).part4.rar

    本书对Java技术的阐述精确到位,叙述方式深入浅出,并包含大量示例,从而帮助读者充分理解Java语言以及Java类库的相关特性。  本书适合软件开发人员、高等院校教师和学生参考。 本资源共分5部分,分别为: JAVA....

Global site tag (gtag.js) - Google Analytics