Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SE 11 String Final Variable with Ternary Operator Does Not Count as a Constant Variable in Switch Case Expression

I encountered a problem that the following code doesn't work. I ran the code in Java SE 11 (11.0.8), Eclipse 2020-06, Windows 10.


Use String Final Variable with Ternary Operator: Doesn't work

public class Tester {
    public static void main(String[] args) {
        
        String switchVar = "abc";
        final String caseStr = true ? "abc" : "def";    
        switch (switchVar) {
            case caseStr: System.out.println("Doesn't work");
        }
    }
}

It has a compile time error: java.lang.Error: Unresolved compilation problem: case expressions must be constant expressions.


However, according to JLS §4.12.4 and JLS §15.28, the String type can be a final variable and ternary operator can also be counted as constant expression.

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression.

A 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:

...

  • The ternary conditional operator ? :

  • Simple names that refer to constant variables


I did some more tests which showed either one of these points works if not combined together.

Directly use constant expression as case constant: No Problem

public class Tester {
    public static void main(String[] args) {
        
        String switchVar = "abc";
        switch (switchVar) {
            case true ? "abc" : "def": System.out.println("works");
        }
    }
}

Use String constant variable without ternary operator: No Problem

public class Tester {
    public static void main(String[] args) {
        
        String switchVar = "abc";
        
        final String VAR_A = "a";
        final String VAR_BC = "bc";
        final String CASE = VAR_A + VAR_BC;
        
        switch (switchVar) {
            case CASE : System.out.println("works");
        }
    }
}

Use int with ternary operator instead of String: No Problem

public class Tester {
    public static void main(String[] args) {
        
        int switchVar = 10;
        final int CASE = 3 > 2 ? 10 : 0;
        
        switch (switchVar) {
            case CASE : System.out.println("works");
        }
    }
}

Could anyone help me please?

like image 958
Frank Mi Avatar asked Aug 24 '20 14:08

Frank Mi


People also ask

How do you handle 3 conditions in a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Can we use ternary operator in if condition in Java?

Java ternary operator is the only conditional operator that takes three operands. It's a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators.

Can ternary operator return string?

Ternary operator values The values part of the ternary operator in the above example is this: “This is an even number!” : “This is an odd number!”; In the example above, if the condition evaluates to true then the ternary operator will return the string value “This is an even number!”.

Is Java ternary operator lazy?

It's worth mentioning that the operator is lazy in the sense that only the used expression is evaluated: The ternary operator will not evaluate the unused branch.


1 Answers

With kindly help of others, it is sure now this is a bug of eclipse.

I have reported the bug to eclipse. (Bugzilla – Bug 566332)

like image 92
Frank Mi Avatar answered Oct 14 '22 09:10

Frank Mi