Compile Time Evaluation: constexpr for Java

Remi Forax forax at univ-mlv.fr
Tue Jan 12 08:35:04 UTC 2021


> De: "Suminda Sirinath Salpitikorala Dharmasena" <sirinath1978m at gmail.com>
> À: "compiler-dev" <compiler-dev at openjdk.java.net>
> Envoyé: Mardi 12 Janvier 2021 07:27:03
> Objet: Compile Time Evaluation: constexpr for Java

> Hello,

> Since the const keyword is not used in Java can this be operposed for compile
> time evaluation? Classes, interfaces, methods, fields marked with const will
> only be available at compile time and would be fully evaluated and replaced
> with the results in runtime bytecode.

Java and the JVM already has several kind of constants, you have the constant for the compiler, but you have also the constant for the interpreter and the constant for the JIT. 
Because you have separate compilation of classes in Java, compile time constants are not used a lot. 

We tend to use static final field which are constant for the JIT (or the compiler if it's a primitive or a String). 
If you want more exotic kind of constants, you can build a constant method handle tree (see the package java.lang.invoke) that let you define whatever you want as constant (see my answer to your other mail). 

The JVM has another kind of constants, constantdynamic, which are constant for the interpreter (and the JITs) but currently there is no way to express such kind of constants in Java. 
see https://bugs.openjdk.java.net/browse/JDK-8209964 

> Suminda

regards, 
Rémi 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210112/142faf4b/attachment-0001.htm>


More information about the compiler-dev mailing list