java - Compile time constants and variables -
java - Compile time constants and variables -
the java language documentation says "if primitive type or string defined constant , value known @ compile time, compiler replaces constant name everywhere in code value. called compile-time constant."
so understanding if have piece of code:
private final int x = 10; then, compiler replace every occurrence of 'x' in code literal '10'. suppose constant initialized value @ run-time,
private final int x = getx(); // here getx() returns integer value @ run-time. will there performance drop (howsoever negligible) may compared compile-time constant?
another question whether below line of code:
private int y = 10; // here y not final is treated in same way compile time constant compiler?
edit: finally, understand answers are:
final static means compile time constant just final means constant initialized @ run-time just static means initialized @ run-time without final variable , wouldn't treated constant. is understanding correct?
compile time constant must be:
declared final primitive or string initialized within declaration initialized constant expressionso private final int x = getx(); not constant.
to sec question private int y = 10; not constant (non-final in case), optimizer cannot sure value not alter in future. cannot optimize constant value. reply is: no, not treated same way compile time constant.
java performance constants final compile-time-constant
Comments
Post a Comment