is there any concept called “Constant Folding” in java?

前端 未结 2 421
时光说笑
时光说笑 2020-12-04 03:23

is there any concept called \"Constant Folding\" in java? if yes what is it?

2条回答
  •  醉梦人生
    2020-12-04 04:00

    Yes, there is.

    From this JavaWorld article (which you could've googled yourself!):

    static final int length = 25;
    static final int width = 10;
    int res = length * width;
    

    Execution time is not used to multiply those values; instead, multiplication is done at compile time. The code for the following variable assignment is modified to produce bytecode that represents the product of width and length:

    int res  = 250;
    

提交回复
热议问题