<small id='hD2Rw'></small><noframes id='hD2Rw'>

  • <tfoot id='hD2Rw'></tfoot>

      • <bdo id='hD2Rw'></bdo><ul id='hD2Rw'></ul>
        <i id='hD2Rw'><tr id='hD2Rw'><dt id='hD2Rw'><q id='hD2Rw'><span id='hD2Rw'><b id='hD2Rw'><form id='hD2Rw'><ins id='hD2Rw'></ins><ul id='hD2Rw'></ul><sub id='hD2Rw'></sub></form><legend id='hD2Rw'></legend><bdo id='hD2Rw'><pre id='hD2Rw'><center id='hD2Rw'></center></pre></bdo></b><th id='hD2Rw'></th></span></q></dt></tr></i><div id='hD2Rw'><tfoot id='hD2Rw'></tfoot><dl id='hD2Rw'><fieldset id='hD2Rw'></fieldset></dl></div>
      1. <legend id='hD2Rw'><style id='hD2Rw'><dir id='hD2Rw'><q id='hD2Rw'></q></dir></style></legend>

        Lambda 表达式如何在 Java 字节码中翻译

        How Lambda Expressions Are Translate In Java Byte Code(Lambda 表达式如何在 Java 字节码中翻译)
        <legend id='PNfnm'><style id='PNfnm'><dir id='PNfnm'><q id='PNfnm'></q></dir></style></legend>
          <tbody id='PNfnm'></tbody>

          <tfoot id='PNfnm'></tfoot>
            • <bdo id='PNfnm'></bdo><ul id='PNfnm'></ul>
              <i id='PNfnm'><tr id='PNfnm'><dt id='PNfnm'><q id='PNfnm'><span id='PNfnm'><b id='PNfnm'><form id='PNfnm'><ins id='PNfnm'></ins><ul id='PNfnm'></ul><sub id='PNfnm'></sub></form><legend id='PNfnm'></legend><bdo id='PNfnm'><pre id='PNfnm'><center id='PNfnm'></center></pre></bdo></b><th id='PNfnm'></th></span></q></dt></tr></i><div id='PNfnm'><tfoot id='PNfnm'></tfoot><dl id='PNfnm'><fieldset id='PNfnm'></fieldset></dl></div>

                  <small id='PNfnm'></small><noframes id='PNfnm'>

                  本文介绍了Lambda 表达式如何在 Java 字节码中翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 java 中使用 lambda 表达式创建一个示例,并且我使用的是官方 JDK8.我的示例运行成功.但是当我试图检查编译器如何将 lambda 表达式转换为字节码时,这让我有些困惑.以下是我的示例代码:-

                  I am trying to create an example using lambda expression in java and i am using offical JDK8. My example was run successfully. But when i trying to check how the compiler translate lambda expression into byte code, this makes me some confusion.Following is the code of my example:-

                  public class LambdaTest {
                      public Integer lambdaBinaryOpertor(BinaryOperator<Integer> binaryOperator) {
                          return binaryOperator.apply(60, 72);
                      }
                  
                      public static void main(String[] args) {
                          LambdaTest test = new LambdaTest();
                          BinaryOperator<Integer> binaryOperator = (a, b) -> a*b;
                          System.out.println("Additon using Lambda BinaryOperator: "+test.lambdaBinaryOpertor(binaryOperator));
                      }
                  }
                  

                  在这篇文章中,他们讨论了编译器如何将 lambda 表达式转换为字节码.根据该文档,lambda 表达式转换为 static 方法和 lambda 表达式声明的位置,有 lambda static 方法的引用.以下示例在文章中:

                  In this Article, they discuss about how compiler translate the lambda expressions into byte code. According to this document the lambda expression convert into static method and the location where lambda expression declare, have reference of lambda static method. The following example is in the article :

                  //Source code
                  class A {
                      public void foo() {
                          List<String> list = ...
                          list.forEach( s -> { System.out.println(s); } );
                      }
                  } 
                  
                  //After compile above code "translate code " 
                  class A {
                      public void foo() {
                          List<String> list = ...
                          list.forEach( [lambda for lambda$1 as Block] );
                      }
                  
                      static void lambda$1(String s) {
                          System.out.println(s);
                      }
                  }
                  

                  我的示例运行良好,并为我们提供了适当的结果.但是当我尝试运行 javap 命令来检查类的字节码时,字节码中没有用于 lambdas 的静态方法.

                  My example run fine and give us the appropriate result. But when i trying to run the javap command for check the byte code of class, there is no static method for lambdas in byte code.

                  c:>javap LambdaTest
                  Compiled from "LambdaTest.java"
                  public class LambdaTest {
                  public LambdaTest();
                  public java.lang.Integer lambdaBinaryOpertor(java.util.function.BinaryOperator <java.lang.Integer>);
                  public static void main(java.lang.String[]);
                  }
                  

                  在泛型的情况下,桥接方法是由编译器创建的,我们也将使用 javap 命令检查此方法,但在 lambdas 的情况下,没有 static方法.文章发表于 2012 年,java 8 于 2014 年 3 月发布.所以我对 lambda 的跨国有一些疑问:

                  In the case of generics, bridge method is created by compiler and we will also check this method using javap command, but in the case of lambdas there is no static method. The article was published at 2012 and java 8 was launch at march 2014. So i have some queries regarding transnational of lambda:

                  1. 在本文发布之后,JDK 8 中是否有任何用于 lambda 的新实现,否则我会在检查 lambda 方法时做错什么?
                  2. 编译器如何处理 lambda 表达式?
                  3. JVM如何调用lambda表达式?

                  推荐答案

                  使用 javap 附加参数来打印关于类的完整信息:javap -v -p -s -c

                  Use javap additional arguments to print full information about class: javap -v -p -s -c

                  对于您的示例,lambda 的源代码将是:

                  For your example, source code of lambda will be:

                  private static java.lang.Integer lambda$main$0(java.lang.Integer, java.lang.Integer);
                      descriptor: (Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;
                      flags: ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
                      Code:
                        stack=2, locals=2, args_size=2
                           0: aload_0       
                           1: invokevirtual #17                 // Method java/lang/Integer.intValue:()I
                           4: aload_1       
                           5: invokevirtual #17                 // Method java/lang/Integer.intValue:()I
                           8: imul          
                           9: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
                          12: areturn       
                        LineNumberTable:
                          line 10: 0
                        LocalVariableTable:
                          Start  Length  Slot  Name   Signature
                              0      13     0     a   Ljava/lang/Integer;
                              0      13     1     b   Ljava/lang/Integer;
                  }
                  

                  这篇关于Lambda 表达式如何在 Java 字节码中翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                  <i id='ZVpmI'><tr id='ZVpmI'><dt id='ZVpmI'><q id='ZVpmI'><span id='ZVpmI'><b id='ZVpmI'><form id='ZVpmI'><ins id='ZVpmI'></ins><ul id='ZVpmI'></ul><sub id='ZVpmI'></sub></form><legend id='ZVpmI'></legend><bdo id='ZVpmI'><pre id='ZVpmI'><center id='ZVpmI'></center></pre></bdo></b><th id='ZVpmI'></th></span></q></dt></tr></i><div id='ZVpmI'><tfoot id='ZVpmI'></tfoot><dl id='ZVpmI'><fieldset id='ZVpmI'></fieldset></dl></div>
                    <tbody id='ZVpmI'></tbody>

                  • <small id='ZVpmI'></small><noframes id='ZVpmI'>

                    • <legend id='ZVpmI'><style id='ZVpmI'><dir id='ZVpmI'><q id='ZVpmI'></q></dir></style></legend>

                          <bdo id='ZVpmI'></bdo><ul id='ZVpmI'></ul>
                            <tfoot id='ZVpmI'></tfoot>