Why isn't null a compile time constant?

2020-04-02 06:30发布

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).

2条回答
Evening l夕情丶
2楼-- · 2020-04-02 07:09

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:

查看更多
Viruses.
3楼-- · 2020-04-02 07:15

In your case the CONSTANT is not compile time constant.

Compile-time constant is a constant and its value is known at compile time and it won’t change further, then the compiler replaces the constant name everywhere in the code with its value .

Generally a primitive type or a string literal that is declared with final is treated as compile time constant by the compiler. Example:

final int a=10;
final String constant =”this is compile time const”;

These both are compile time constants we can use Compile-time constant expressions in case labels of switch statements

Example for non compile time constants

final String xyz = new String(”this is not a compile time const");

here xyz string object is not a compile time constant.Because this 'xyz' string object is going to create at run time and here compiler knows about the reference only not the string object.

The same is applicable to your static final Object CONSTANT = null

Accroding to JLS

Type of the null literal is the null type;its value is the null reference.

查看更多
登录 后发表回答