Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `1/0` a constant expression in Java? [duplicate]

As far as I understand the Java 8 JLS the expression (1/0) is considered a constant expression, but when I try to compile the following program with OpenJDK 8 I get an error

public class Switch {     public static void main(String[] args) {         switch(42) {             case (1/0):                 return;             default:                 return;         }     } } 

The error says (1/0) isn't a constant expression

Switch.java:4: error: constant expression required             case (1/0):                  ^ 1 error 

Am I missing something? Or is it a bug in OpenJDK 8?

like image 897
ReyCharles Avatar asked Jul 03 '15 12:07

ReyCharles


People also ask

What is a constant expression in Java?

A constant expression is an expression that always produces the same result. More precisely, a constant expression is an expression that produces a pure value of a primitive data type and is only composed of the following: Literals of primitive data types. String literals.

What is constant expression?

A constant expression is an expression that can be evaluated at compile time. Constants of integral or enumerated type are required in several different situations, such as array bounds, enumerator values, and case labels. Null pointer constants are a special case of integral constants.

What does 0 mean in Java?

'0' is the char value of zero. When you write a string, you're writing an array of 'char' datatypes which the compiler translates into ASCII values (which have a corresponding decimal number value).

What is constant expression required?

A constant must be initialized. This error has the following causes and solutions: You tried to initialize a constant with a variable, an instance of a user-defined type, an object, or the return value of a function call.


2 Answers

The compiler is doing constant folding (precomputing trivial literal expressions). This is a case where the expression "completes abruptly", to use the JLS verbiage, disqualifying it from meeting the definition of "constant expression". So it's not a bug, it's consistent with the JLS.

And yes, the expression doesn't evaluate to a value either (warning the user trying to do something like this that the result will not be a constant expression), but the compiler doesn't know that until it tries. Not evaluating to a value and completing abruptly would seem to go hand-in-hand.

Adding a variable declaration like

int x = 1 / 0; 

doesn't cause a compiler error, it's the switch that forces the expression to be evaluated at compile time.

By the way I checked that this happens for version 7 of the Oracle and IBM JDKs too, it's not OpenJDK or JDK8 specific.

like image 168
Nathan Hughes Avatar answered Oct 12 '22 10:10

Nathan Hughes


A constant expression must be able to evaluate to a value, since the compiler must reconduce that expression to a value.

1/0 doesn't have any value.

From JSL §15.28:

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

like image 35
Jack Avatar answered Oct 12 '22 11:10

Jack