Java Lambdas:它如何在JVM&是OOP吗? [英] Java Lambdas : How it works in JVM & is it OOP?

查看:177
本文介绍了Java Lambdas:它如何在JVM&是OOP吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在匿名内部类的情况下,传递(匿名)对象引用并执行该对象的方法。

For example in the case of an anonymous inner class, an (anonymous) object reference is passed and methods of that object are executed.

Lambdas是代码块,将在需要时执行。

Lambdas are code blocks which will be executed when needed.

遇到lambdas时JVM中会发生什么? JVM在哪里存储与lambdas相关的代码块(Heap:Young,Old或Permanent Generation)?

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中发生的事情,因为在JAVA中,一切都是基于对象的。

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.


  1. 所以在OOP的上下文中lambda是如何工作的?

  1. So in context of OOP how do lambdas work?

lambda违反OOP概念吗?

Do lambdas violate OOP concepts?

Lambda是否合适对于垃圾收集器,因为没有创建对象因此
不用担心内存问题和清​​除内存?

Is Lambda good for the garbage collector as no objects are created hence no worry about memory issues and clearing memory?


推荐答案

我不会浪费时间思考天气lambda表达式违反OO原则。它的目标是增加语言的功能而不是编写OO代码,我不知道lambdas如何违反封装,继承或多态。

I wouldn't waste my time thinking weather 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.

这个< a href =http://blog.takipi.com/compiling-lambda-expressions-scala-vs-java-8/>文章解释了Java如何处理lambda表达式:

This article explains how Java handles lambda expressions:


Lambda表达式的有趣之处在于,从JVM的角度来看,它们是完全不可见的。它没有匿名函数或Lambda表达式的概念。它只知道字节码是严格的OO规范。由语言及其编译器的制造商在这些约束条件下工作以创建更新,更高级的语言元素。

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.

考虑到以下代码:

List names = Arrays.asList("1", "2", "3");
Stream lengths = names.stream().map(name -> name.length());




...通过加载名称var和invokes开始它的 .stream <)方法,但它做了一件非常优雅的事情。它不使用创建将包装Lambda函数的新对象,而是使用Java 7中添加的新 invokeDynamic 指令将该调用站点动态链接到实际的Lambda函数。

... 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 new invokeDynamic instruction which was added in Java 7 to dynamically link this call site to the actual Lambda function.



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 是一个在Java 7中添加的指令,它使JVM不那么严格,并允许动态语言在运行时绑定符号,而不是在JVM编译代码时静态地执行所有链接。

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.

Lambda代码

aload_0
invokevirtual java/lang/String.length:()
invokestatic java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
areturn

这篇关于Java Lambdas:它如何在JVM&amp;是OOP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆