Dominance and primitive type patterns

Cay Horstmann cay at horstmann.com
Fri Aug 2 11:36:55 UTC 2024


I was just asked whether the following is the intended behavior or a bug:

short x = 404;
switch (x) {
   case short _ -> System.out.println("It's a short");
   case 404     -> System.out.println("It's 404"); // <-- not dominated
}

If one uses case (short) 404, dominance works as expected.

And while I am at it:

float x = 404;
switch (x) {
   case 404.    -> System.out.println("It's 404");
   case float _ -> System.out.println("It's a float");
}

gives an error in JShell

incompatible types: possible lossy conversion from double to float

Is that the intended behavior? Obviously there is no possibility of lossy conversion here.

Cheers,

Cay
-- 

--

Cay S. Horstmann | https://horstmann.com


More information about the amber-spec-observers mailing list