mistaking switch expressions and switch statements

Cay Horstmann cay.horstmann at gmail.com
Sat Feb 5 07:00:52 UTC 2022


The article has other issues. Figure 6 is captioned "Handling complex 
situations with a switch expression" but contains a switch *statement* 
without fall-through.

Another example where the two axis design (statement vs. expression, 
fall-through vs. no fall-through) has not been properly understood.

Cheers,

Cay

On 04/02/2022 22:59, Brian Goetz wrote:
> 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 
>>
> 

-- 

Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com


More information about the amber-dev mailing list