Is `1/0` a constant expression in Java?

ReyCharles

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?

Nathan Hughes

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does "int size = 10;" yield a constant expression?

From Java

Why this constant expression is not constant

From Dev

Expression is not an integer constant expression

From Dev

How to use Java Constant as a part of @Scheduler expression annotation in Spring?

From Dev

How to call java constant variables from jsp expression language in Spring?

From Dev

constant expression required in Java behaviour change for int and Integer

From Dev

Java switch statement says constant expression required even with case 1

From Dev

`core constant expression` vs `constant expression`

From Dev

Is a glvalue integral constant expression a constant expression?

From Dev

Is a glvalue integral constant expression a constant expression?

From Dev

Understanding constant expression

From Dev

Dividing by zero in a constant expression

From Dev

Why is this not a constant expression?

From Dev

understand a constant expression error

From Dev

Constant expression required

From Dev

Why is this an illegal constant expression?

From Dev

Adding a constant to a closure expression

From Dev

Constant expression required

From Dev

"expected constant expression" Error

From Dev

Android "constant expression required"

From Dev

Why does Java Language Specification say that the expression (n > 2) is not a constant expression?

From Dev

Java regular expression with 10+ groups

From Dev

in F#, when is a "constant string expression" not a "constant string expression"

From Dev

Clarification of converted constant expression definition

From Java

__PRETTY_FUNCTION__ in constant expression

From Dev

Expression to check if a property is equal to a constant

From Dev

__builtin_round is not a constant expression

From Dev

c++: "expected constant expression"

From Dev

Case expressions must be constant expression

Related Related

  1. 1

    Does "int size = 10;" yield a constant expression?

  2. 2

    Why this constant expression is not constant

  3. 3

    Expression is not an integer constant expression

  4. 4

    How to use Java Constant as a part of @Scheduler expression annotation in Spring?

  5. 5

    How to call java constant variables from jsp expression language in Spring?

  6. 6

    constant expression required in Java behaviour change for int and Integer

  7. 7

    Java switch statement says constant expression required even with case 1

  8. 8

    `core constant expression` vs `constant expression`

  9. 9

    Is a glvalue integral constant expression a constant expression?

  10. 10

    Is a glvalue integral constant expression a constant expression?

  11. 11

    Understanding constant expression

  12. 12

    Dividing by zero in a constant expression

  13. 13

    Why is this not a constant expression?

  14. 14

    understand a constant expression error

  15. 15

    Constant expression required

  16. 16

    Why is this an illegal constant expression?

  17. 17

    Adding a constant to a closure expression

  18. 18

    Constant expression required

  19. 19

    "expected constant expression" Error

  20. 20

    Android "constant expression required"

  21. 21

    Why does Java Language Specification say that the expression (n > 2) is not a constant expression?

  22. 22

    Java regular expression with 10+ groups

  23. 23

    in F#, when is a "constant string expression" not a "constant string expression"

  24. 24

    Clarification of converted constant expression definition

  25. 25

    __PRETTY_FUNCTION__ in constant expression

  26. 26

    Expression to check if a property is equal to a constant

  27. 27

    __builtin_round is not a constant expression

  28. 28

    c++: "expected constant expression"

  29. 29

    Case expressions must be constant expression

HotTag

Archive