Compile-time calculation: Difference between revisions

m
Added more explanation about Java's compile time limitations.
(→‎{{header|Raku}}: rephrasing and removing parenthesis)
m (Added more explanation about Java's compile time limitations.)
Line 838:
 
=={{header|Java}}==
The Java compiler is able to calculate expressions that contain constant variables
Compile time calculation of constant values is possible in Java.
and certain operators during code compilation.
As defined in the Java language specification,
the following operators and expressions may be used for constant expressions:
Unary operators: +, -, ~, !
Multiplicative operators: *, /, %
Additive operators: +, –
Shift operators: <<, >>, >>>
Relational operators: <, <=, >, >=
Equality operators: ==, !=
Bitwise and logical operators: &, ^, |
Conditional-and and the conditional-or operator: &&, ||
Ternary conditional operator: ?:
Parenthesized expressions whose contained expression is a constant expression
Simple names that refer to constant variables
<syntaxhighlight lang="java">
 
import java.util.List;
import java.util.stream.IntStream;
 
public final class CompileTimeCalculation {
Line 848 ⟶ 860:
public static void main(String[] aArgs) {
System.out.println(tenFactorial);
System.out.println(word);
System.out.println(numbers);
}
private static int tenFactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
private static String word = "Street".substring(1, 5);
private static List<Integer> numbers = IntStream.rangeClosed(1, 10).boxed().toList();
 
}
Line 861 ⟶ 869:
<pre>
3628800
tree
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</pre>
 
908

edits