Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't null a compile time constant?

So if I have a static final Object CONSTANT = null, for some reason if I reference that in another piece of code like doSomething(CONSTANT), it won't be in-lined onto the code during compilation. So instead of being doSomething(null) after being compiled, it would be doSomething(CONSTANT).

like image 630
Muhatashim Avatar asked Jul 20 '13 04:07

Muhatashim


People also ask

Is a compile time constant?

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.

What is compile time constant in C#?

Constants are immutable values which are known at compile time and do not change for the life of the program. Constants are declared with the const modifier. Only the C# built-in types (excluding System. Object) may be declared as const .

What is compile time variable?

A Java variable is a compile-time constant if it's of a primitive type or String, declared final, initialized within its declaration, and with a constant expression. Strings are a special case on top of the primitive types because they are immutable and live in a String pool.

Is a constant value of certain type?

In mathematics, a constant is a specific number or a symbol that is assigned a fixed value. In other words, a constant is a value or number that never changes in expression. Its value is constantly the same. Examples of constant are 2, 5, 0, -3, -7, 2/7, 7/9 etc.


1 Answers

Your CONSTANT is not a compile time constant because the JLS says it is not. The only types that can be used in constant expressions are the primitive types and String.

The sense of it is that an Object instance (in general) has a semantically significant object identity that distinguishes it from other Object instances. This Object identity cannot be encoded in a class file ... or at least, it can't be encoded with the current classfile formats. (And if it could, there would be all sorts of other problems ...)

The value null could (in theory) be handled as a special case, except that there is not a great deal of point. Specifically, you can't use null in any of the contexts where a "compile time constant" is required (or advantageous) from the linguistic perspective. For instance:

  • You can't have null as a case expression.
  • Since == for reference types is not a constant expression, you can't use it for the Java "conditional compilation" idiom involving an if with a constant expression as a condition. (And besides null == null is not a useful condition ...)

As far as inlining is concerned, while the "constant" cannot be inlined in the bytecodes (because of the JLS rules about what a "constant expression" is), the JIT compiler's optimizer would be permitted to do this, and may actually do it ... if there are tangible performance benefits.

Reference:

  • JLS 15.28 - Constant Expressions
like image 51
Stephen C Avatar answered Oct 09 '22 01:10

Stephen C