Will the Java compiler precalculate sums of literals?
问题 int i = 10 + 20; Is it true that the compiler will process this code, adding 10 + 20 , and the byte code is the same as for this line of code? int i = 30; Where can I read about it? 回答1: Yes, and you can even verify it for yourself. Take a small Java file, for example: public class Main { public Main() { int i = 10 + 20; } } Compile it with javac Main.java , and then run javap -c Main to disassemble it: Compiled from "Main.java" public class Main extends java.lang.Object{ public Main(); Code: