mistaking switch expressions and switch statements

Brian Goetz brian.goetz at oracle.com
Fri Feb 4 21:59:14 UTC 2022


You are correct that the default can be elided here, because this is a 
switch statement on a legacy target type whose labels are all constants, 
and such statements (for reasons of backward compatibility) can be 
partial.  All other switches (new target types, new cases, expressions) 
should be total.

The example actually employs "belts and suspenders"; it initializes 
`discount` to zero, and then assigns it to zero in the default clause.  
If discount were left blank, then the default clause would be necessary, 
not to make the switch total, but to make `discount` definitely 
assigned.  So you could either elide the initializer on `discount`, or 
the default clause, but not both.

On 2/4/2022 4:31 PM, Mateusz Romanowski wrote:
> Hi,
> I am reading article "Design implications of Java’s switch statements and
> switch expressions" [1] by Vasily STRELNIKOV .
>
> I am confounded by "*Figure 6.* Handling complex situations with a Switch
> Expression" which I believe is a new-style Switch Statement as the `default
> clause` is removable even though Switch is not total.
>
> Could you confirm my interpretation?
> And should it be correct, help to contact author to ask for correction.
>
> Cheers,
> Mateusz Romanowski
>
> [1]
> https://blogs.oracle.com/javamagazine/post/java-switch-statements-expressions



More information about the amber-dev mailing list