<div dir="ltr">Are you using a Java version that supports pattern matching for switch (JDK 17+ as a preview feature, standardized in JDK 21)? Or, it is a bug.</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Fri, Sep 5, 2025 at 7:09 AM Simon Ritter <<a href="mailto:sritter@azul.com">sritter@azul.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
I've been giving a presentation on Patterns in the Java language and <br>
including some puzzles.  The recent inclusion of Primitive Types in <br>
Patterns has provided some interesting material.  I currently have one <br>
puzzle that I can't quite explain; hopefully someone on the mailing list <br>
can provide clarification.<br>
<br>
Let's start with this simple example:<br>
<br>
   int x = getX();  // x is 42<br>
<br>
   switch (x) {<br>
     case byte b -> System.out.println("byte");<br>
     case int i -> System.out.println("int");<br>
   }<br>
<br>
Here we have a runtime check, which establishes that the conversion from <br>
int to byte is exact, as there is no loss of information.<br>
<br>
If we reverse the order of the cases:<br>
<br>
   switch (x) {<br>
     case int i -> System.out.println("int");<br>
     case byte b -> System.out.println("byte");<br>
   }<br>
<br>
The code will not compile, as the int case dominates the byte case.<br>
<br>
So far, so good.<br>
<br>
However, if we change the int case to use a wrapper class:<br>
<br>
   switch (x) {<br>
     case Integer i -> System.out.println("int");<br>
     case byte b -> System.out.println("byte");<br>
   }<br>
<br>
the code will compile and the result is 'int'.<br>
<br>
If I look at JEP 507, under the section on Safety of conversions, it <br>
states that "...boxing conversions and widening reference conversions <br>
are unconditionally exact."  The compiler is autoboxing the int, x, to <br>
create an Integer object, which always matches the first case.<br>
<br>
What I can't explain is why the compiler does not still see this as <br>
pattern dominance?  No value of x will ever result in the switch <br>
matching on byte so the code is unreachable.<br>
<br>
Thanks in advance,<br>
<br>
Simon.<br>
</blockquote></div>