【问题标题】:How does + operator work in Java?+ 运算符在 Java 中是如何工作的?
【发布时间】:2015-09-03 03:41:30
【问题描述】:

我在下面提供了一些带有其编译类的代码。

public class PlusOperator {

public static void main(String[] args) {

    int a=10;
    int b=20;
    int c =a+b;

    String s1 = 1+3+"f";
    String s2 = "f"+1+2;
    String s3 = (1+3)+"f";
    String s4 = "f"+(1+3);

}
}

用 Java 1.6 编译的类

  // Compiled from PlusOperator.java (version 1.6 : 50.0, super bit)
public class question.PlusOperator {

  // Method descriptor #6 ()V
  // Stack: 1, Locals: 1
  public PlusOperator();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [8]
    4  return
      Line numbers:
        [pc: 0, line: 3]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: question.PlusOperator

  // Method descriptor #15 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 8
  public static void main(java.lang.String[] args);
     0  bipush 10
     2  istore_1 [a]
     3  bipush 20
     5  istore_2 [b]
     6  iload_1 [a]
     7  iload_2 [b]
     8  iadd
     9  istore_3 [c]
    10  ldc <String "4f"> [16]
    12  astore 4 [s1]
    14  ldc <String "f12"> [18]
    16  astore 5 [s2]
    18  ldc <String "4f"> [16]
    20  astore 6 [s3]
    22  ldc <String "f4"> [20]
    24  astore 7 [s4]
    26  return
      Line numbers:
        [pc: 0, line: 7]
        [pc: 3, line: 8]
        [pc: 6, line: 9]
        [pc: 10, line: 11]
        [pc: 14, line: 12]
        [pc: 18, line: 13]
        [pc: 22, line: 14]
        [pc: 26, line: 16]
      Local variable table:
        [pc: 0, pc: 27] local: args index: 0 type: java.lang.String[]
        [pc: 3, pc: 27] local: a index: 1 type: int
        [pc: 6, pc: 27] local: b index: 2 type: int
        [pc: 10, pc: 27] local: c index: 3 type: int
        [pc: 14, pc: 27] local: s1 index: 4 type: java.lang.String
        [pc: 18, pc: 27] local: s2 index: 5 type: java.lang.String
        [pc: 22, pc: 27] local: s3 index: 6 type: java.lang.String
        [pc: 26, pc: 27] local: s4 index: 7 type: java.lang.String
}

我的问题:

  1. + 运算符如何与 String 和 int 一起使用?
  2. 如果您看到已编译的代码,则在编译时也会使用 int 评估字符串“+”,但如果是“+”,则仅在运行时执行 int。为什么?

【问题讨论】:

  • 试试 int c = 10+20;我认为区别在于常量与变量,而不是字符串 + 与 int +。
  • 您的所有String 变量都使用常量表达式进行初始化。您的 c 变量未使用常量表达式初始化。

标签: java


【解决方案1】:

ab 是变量——它们的值只有在运行时才知道1。但是,1f3 是常量,因此编译器足够聪明地计算结果并将其包含在字节码中。所以像3 + f 这样的表达式会自动编译成常量3f

这与intString 类型的操作数无关。用Strings 试试同样的方法:

String str = a + b;

您会在编译后的代码中看到对StringBuilder#append(String) 的调用。


1 一个例外是当变量被声明为final,所以在下面的代码中:

final int a=10;
final int b=20;
int c = a + b;

c在字节码中直接赋值为30

bipush 10
istore_1
bipush 20
istore_2
bipush 30
istore_3

【讨论】:

  • 感谢您的评论,现在对它有了一些清晰的了解。
【解决方案2】:

您正在了解编译器如何工作的语义。它正在解析您的代码,然后在可行的情况下将其转换为字节码。如果在字符串中使用“+”运算符,如果在二进制表达式中使用,它会以一种方式起作用,那么它会以另一种方式起作用。运算符重载。

  1. “+”运算符被重载。

  2. 这是在编译器中完成的代码优化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多