2

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

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • 1
    @fredrk they may be similar or simple. but i dont think there is no harm in getting answers here. so that i will be comnfirmed. and i may get explained answers – GuruKulki Jan 06 '10 at 11:41
  • 1
    well, it is wrong to ask questions here if google has _the obvious answer_ as a first result. – Bozho Jan 06 '10 at 11:44

2 Answers2

10

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;
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
7

Constant folding is the process of simplifying constant expressions at compile time. Terms in constant expressions are typically simple literals, such as the integer 2, but can also be variables whose values are never modified, or variables explicitly marked as constant

Yes, it's exists on Java: Compiler optimizations

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 2
    it appeared to me that he knows what constant folding is and is asking whether it exists in java. – Bozho Jan 06 '10 at 11:32