Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile-time constants and variables

The Java language documentation says:

If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time constant.

My understanding is if we have a piece of code:

private final int x = 10; 

Then, the compiler will replace every occurrence of x in the code with literal 10.


But suppose the constant is initialized at run-time:

private final int x = getX(); // here getX() returns an integer value at run-time. 

Will there be any performance drop (howsoever negligible it may be) compared to the compile-time constant?


Another question is whether the below line of code:

private int y = 10; // here y is not final 

is treated in same way as compile-time constant by the compiler?


Finally, what I understand from the answers are:

  1. final static means compile-time constant
  2. just final means it's a constant but is initialized at run-time
  3. just static means initialized at run-time
  4. without final is a variable and wouldn't be treated as constant.

Is my understanding correct?

like image 926
AllTooSir Avatar asked Jan 31 '12 16:01

AllTooSir


People also ask

What are compile time constants?

A compile-time constant is a value that can be (and is) computed at compile-time. A runtime constant is a value that is computed only while the program is running. If you run the same program more than once: A compile-time constant will have the same value each time the application is run.

What is compile time constant in Dart?

Dart has the concept of compile-time constants. A compile-time constant is parsed and created at compile time, and canonicalized. For example, here is a const constructor for Point: class Point { final num x, y; const Point(this.x, this.y); }

What determines compile time?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is compile time and runtime C++?

A compile-time constant is a value that is computed at the compilation-time. Whereas, A runtime constant is a value that is computed only at the time when the program is running. 2. A compile-time constant will have the same value each time when the source code is run.


1 Answers

Compile time constant must be:

  • declared final
  • primitive or String
  • initialized within declaration
  • initialized with constant expression

So private final int x = getX(); is not constant.

To the second question private int y = 10; is not constant (non-final in this case), so optimizer cannot be sure that the value would not change in the future. So it cannot optimize it as good as constant value. The answer is: No, it is not treated the same way as compile time constant.

like image 170
msi Avatar answered Oct 01 '22 04:10

msi