Does the java compiler pre calculate the sum of text?

int i = 10 + 20;
int i = 10 + 20;

Is it true that the compiler will process this code, add 10 and 20 bytes, and the code is the same as this code line?

int i = 30;

Where can I read it?

Solution

Yes, you can even verify it yourself Take a small java file, for example:

public class Main {
  public Main() {
    int i = 10 + 20;
  }
}

Use javac main Compile it in Java, and then run javap - C main to disassemble it:

Compiled from "Main.java"
public class Main extends java.lang.Object{
public Main();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   bipush  30
   6:   istore_1
   7:   return

}

Obviously, in bytecode, you can see the compiler optimization: bipush 30!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>