日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java 8 语法糖_Java8的lamada算是一种语法糖吗?

發(fā)布時間:2025/3/20 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 8 语法糖_Java8的lamada算是一种语法糖吗? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

語法糖是啥?語法糖沒有一個官方的定義(或者說定義相當(dāng)寬泛),更像是一種觀念:任何讓你寫著爽、但是實(shí)際發(fā)生的和你看到的不一樣的東西都可以“被認(rèn)為”是語法糖。例如最常見的Integer和int的自動裝拆箱操作背后就是語法糖。看上去你在寫

Integer i = 1234;

int j = new Integer(2345);

編譯器在背后偷偷摸摸地幫你加了一些料:

Integer i = Integer.valueOf(1234); // 實(shí)際生成的字節(jié)碼的等價代碼

int j = new Integer(2345).intValue();

在Java代碼的編譯過程中有一步叫做“解語法糖”(desugar),就是做這種偷偷摸摸的事情的。

在JDK的源代碼里搜索"lambda desugar"我們可以很容易找到相關(guān)的解語法糖代碼:https://github.com/frohoff/jdk8u-jdk/blob/master/src/share/classes/java/lang/invoke/LambdaMetafactory.java#L181-L191?github.comhttps://github.com/openjdk/jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java#L81?github.com

/**

* This pass desugars lambda expressions into static methods

*

*

This is NOT part of any supported API.

* If you write code that depends on this, you do so at your own risk.

* This code and its internal interfaces are subject to change or

* deletion without notice.

*/

public class LambdaToMethod extends TreeTranslator {這個pass(類似于“一趟掃描”的意思,在生成抽象語法樹的過程中,編譯器可能要進(jìn)行多趟掃描)將lambda表達(dá)式解語法糖成為靜態(tài)方法。

* @apiNote These linkage methods are designed to support the evaluation

* of lambda expressions and method references in the Java

* Language. For every lambda expressions or method reference in the source code,

* there is a target type which is a functional interface. Evaluating a lambda

* expression produces an object of its target type. The recommended mechanism

* for evaluating lambda expressions is to desugar the lambda body to a method,

* invoke an invokedynamic call site whose static argument list describes the

* sole method of the functional interface and the desugared implementation

* method, and returns an object (the lambda object) that implements the target

* type. (For method references, the implementation method is simply the

* referenced method; no desugaring is needed.)……對Iambda表達(dá)式求值的過程會產(chǎn)生一個對應(yīng)的目標(biāo)類型(函數(shù)接口)的對象。對于每個lambda表達(dá)式的求值,推薦的方法是將lambda方法體解語法糖成為一個方法……

所以,不管你覺得是不是,也不管我覺得是不是,OpenJDK的實(shí)現(xiàn)認(rèn)為它是。

總結(jié)

以上是生活随笔為你收集整理的java 8 语法糖_Java8的lamada算是一种语法糖吗?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。