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.
Asked
Active
Viewed 140 times
0

Chimera
- 5,884
- 7
- 49
- 81

Tushar Paliwal
- 301
- 4
- 11
-
1Division by zero!! May god be with us... – Baz Sep 11 '12 at 19:24
-
He is farseer, has a null talisman that allows divison by zero – huseyin tugrul buyukisik Sep 11 '12 at 19:27
-
possible duplicate of [Is 1/0 a legal Java expression?](http://stackoverflow.com/questions/2934063/is-1-0-a-legal-java-expression) – Greg Hewgill Sep 11 '12 at 19:28
1 Answers
3
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.

Roger Lindsjö
- 11,330
- 1
- 42
- 53

Hot Licks
- 47,103
- 17
- 93
- 151