Java Lambdas:它在 JVM 中是如何工作的,它是 OOP 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29143803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Lambdas : How it works in JVM & is it OOP?
提问by Satty
For example in the case of an anonymous inner class, an (anonymous) object reference is passed and methods of that object are executed.
例如,在匿名内部类的情况下,传递(匿名)对象引用并执行该对象的方法。
Lambdas are code blocks which will be executed when needed.
Lambda 是将在需要时执行的代码块。
What happens in the JVM when lambdas are encountered? Where does the JVM store the code blocks related to lambdas (Heap : Young, Old or Permanent Generation)?
当遇到 lambdas 时,JVM 中会发生什么?JVM 在哪里存储与 lambda 相关的代码块(堆:年轻代、老年代或永久代)?
I tried searching, and I got the syntax for using lambdas but was not able to understand what is happening inside JVM, as in JAVA everything is object-based.
我尝试搜索,并获得了使用 lambdas 的语法,但无法理解 JVM 内部发生的事情,因为在 JAVA 中,一切都是基于对象的。
So in context of OOP how do lambdas work?
Do lambdas violate OOP concepts?
Is Lambda good for the garbage collector as no objects are created hence no worry about memory issues and clearing memory?
那么在 OOP 的上下文中,lambdas 是如何工作的呢?
lambda 是否违反了 OOP 概念?
Lambda 对垃圾收集器有好处,因为没有创建对象,因此不用担心内存问题和清除内存吗?
采纳答案by Maroun
I wouldn't waste my time thinking whether the lambda expressions are a violation of OO principles. Its goal is to increase the power of a language and not to write an OO code, I don't see how lambdas can violate encapsulation, inheritance or polymorphism.
我不会浪费时间思考 lambda 表达式是否违反 OO 原则。它的目标是增加语言的能力而不是编写面向对象的代码,我看不出 lambda 会如何违反封装、继承或多态性。
This articleexplains how Java handles lambda expressions:
这篇文章解释了 Java 如何处理 lambda 表达式:
What's interesting about Lambda expressions is that from the JVM's perspective they're completely invisible. It has no notion of what an anonymous function or a Lambda expression is. It only knows bytecode which is a strict OO specification. It's up to the makers of the language and its compiler to work within these constraints to create newer, more advanced language elements.
Lambda 表达式的有趣之处在于,从 JVM 的角度来看,它们是完全不可见的。它不知道什么是匿名函数或 Lambda 表达式。它只知道字节码,这是一个严格的 OO 规范。这取决于语言的制造者及其编译器在这些约束下工作以创建更新、更高级的语言元素。
Considering the following code:
考虑以下代码:
List names = Arrays.asList("1", "2", "3");
Stream lengths = names.stream().map(name -> name.length());
... It begins quite simply by loading the names var and invokes its
.stream()
method, but then it does something quite elegant. Instead of creating a new object that will wrap the Lambda function, it uses the newinvokeDynamic
instruction which was added in Java 7 to dynamically link this call site to the actual Lambda function.
...它开始时很简单地加载名称 var 并调用它的
.stream()
方法,然后它做了一些非常优雅的事情。它没有创建将包装 Lambda 函数的新对象,而是使用invokeDynamic
Java 7 中添加的新指令将此调用站点动态链接到实际的 Lambda 函数。
aload_1 //load the names var
// call its stream() func
invokeinterface java/util/List.stream:()Ljava/util/stream/Stream;
//invokeDynamic magic!
invokedynamic #0:apply:()Ljava/util/function/Function;
//call the map() func
invokeinterface java/util/stream/Stream.map:
(Ljava/util/function/Function;)Ljava/util/stream/Stream;
InvokeDynamic
is an instruction that was added in Java 7 to make the JVM less strict, and allows dynamic languages to bind symbols at run-time, vs. doing all the linkage statically when the code is compiled by the JVM.
InvokeDynamic
是在 Java 7 中添加的一条指令,用于降低 JVM 的严格性,并允许动态语言在运行时绑定符号,而不是在 JVM 编译代码时静态地进行所有链接。
The Lambda code
拉姆达代码
aload_0
invokevirtual java/lang/String.length:()
invokestatic java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
areturn
回答by Sleiman Jneidi
Lambda expressions don't get translated into anonymous inner classes
, they use invoke dynamicthat was introduced in Java 7 to execute functional methods. Check this out.
Lambda 表达式不会被转换为anonymous inner classes
,它们使用Java 7 中引入的动态调用来执行函数式方法。看看这个。
Do they violate OOP
? I don't think that you should care. Lambdas make your code less verbose, easier to understand, and "easier" to parallelise. And thats what you should care about.
他们违反了OOP
吗?我不认为你应该关心。Lambda 使您的代码不那么冗长,更容易理解,并且“更容易”并行化。这才是你应该关心的。
From Brain Goetz comment:
来自 Brain Goetz 的评论:
We don't get paid to write object-oriented programs or functional programs, we get paid to write working programs.
我们不会因为编写面向对象的程序或函数式程序而获得报酬,而是通过编写工作程序获得报酬。
回答by apangin
- Lambda expression is compiled using
invokedynamic
bytecode. - Lambda implementation is stored in the same class file as a special private method.
- Whether an object is created to invoke lambda depends on the situation. In the trivial cases lambda gets translated to a constant method handle.
- To instantiate a lambda HotSpot creates an anonymous class that implements lambda's functional interface. This class does not belong to any ClassLoader.
- Lambda 表达式是使用
invokedynamic
字节码编译的。 - Lambda 实现作为一个特殊的私有方法存储在同一个类文件中。
- 是否创建对象来调用 lambda 取决于具体情况。在微不足道的情况下,lambda 会被转换为常量方法句柄。
- 要实例化 lambda HotSpot,需要创建一个匿名类来实现 lambda 的功能接口。此类不属于任何 ClassLoader。
See more detailsfrom the specification lead of Lambda Expressions JSR.
从Lambda Expressions JSR的规范负责人处查看更多详细信息。