Tushar Paliwal
Tushar Paliwal

Reputation: 301

Computation of string at run time and compile time

I found that the constant literal is computed at compile time for example:int a=10+20; but when i try to use int b=10/0; i find that it is computed at run time. Can you please tell me why this constant literal is computed at run time? Thank you.

Upvotes: 0

Views: 142

Answers (1)

Hot Licks
Hot Licks

Reputation: 47699

A constant value MAY be computed at compile time, if the compiler can maintain the same semantics as if it were done at run time. If not (eg, the case of divide by zero) then it defers the computation to run time. For more info read JLS 15.28.

Upvotes: 3

Related Questions