Java Tuple2 使用访问器方法和直接调用变量的区别 [英] Java Tuple2 difference between using accessor method and calling the variable directly

查看:84
本文介绍了Java Tuple2 使用访问器方法和直接调用变量的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 代码中使用 Tuple2,我想知道通过 getter 访问值或直接获取变量是否有区别.

I'm using Tuple2 in my Java code, and I was wondering if there was a difference between accessing the values via the getter or just getting the variables directly.

Tuple2<String,String> tuple = new Tuple2<>("Hello", "World");
//getting values directly
String direct = tuple._1;
//using getter
String indirect = tuple._1();

推荐答案

第一个加载一个字段,其中第二个使用 getFieldinvokeVirtual 操作码相对地调用方法.生成的字节码看起来像

The first loads a field where the second invokes the method using getField and invokeVirtual opcodes relatively. The generated byte-code looks like

  13: getfield      #6                  // Field scala/Tuple2._1:Ljava/lang/Object;
  16: checkcast     #7                  // class java/lang/String
  19: astore_2
  20: aload_1
  21: invokevirtual #8                  // Method scala/Tuple2._1:()Ljava/lang/Object;
  24: checkcast     #7                  // class java/lang/String

区别在于字段读取和方法调用之间的区别,即 JIT 编译器很乐意内联该方法,这对性能没有太大影响.

And the difference is the difference between a field read and a method invocation, ie the JIT compiler is happy to inline the method and it wouldn't matter much performance wise.

这篇关于Java Tuple2 使用访问器方法和直接调用变量的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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