GuruKulki
GuruKulki

Reputation: 26428

is there any concept called "Constant Folding" in java?

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

Upvotes: 4

Views: 1530

Answers (2)

Rubens Farias
Rubens Farias

Reputation: 57976

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

Upvotes: 7

Bozho
Bozho

Reputation: 597324

Yes, there is.

From this JavaWorld article:

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;

Upvotes: 11

Related Questions