<div dir="ltr">Detailed answer:<div>The compiler does check for pattern dominance to prevent unreachable code; however, it uses the formal definition of pattern dominance.  A pattern P1 (the first case) dominates a pattern P2 (the second case) if every possible value that matches P2 would also match P1. The compiler determines pattern dominance by comparing the patterns themselves, without considering the type of the variable (int x) being switched on.<br><br>Pattern P1: Integer i<br>Pattern P2: byte b<br><br>Does every value that matches<i> byte b</i> also match <i>Integer i</i>? No. <br><br>A value matches <i>byte b</i> if it's an instance of the <i>Byte</i> wrapper class. A value matches <i>Integer i</i> if it's an instance of the <i>Integer</i> wrapper class. Since a <i>Byte</i> object is never an <i>Integer</i> object (they are separate classes),   <i>Integer i</i> does not dominate <i>byte b</i>.<br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Fri, Sep 5, 2025 at 9:15 AM Deepak Vohra <<a href="mailto:dvohra16@gmail.com">dvohra16@gmail.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"><div dir="ltr">The <i>Integer</i> can't auto-box <i>byte</i>; therefore, it is not pattern dominance.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 5, 2025 at 9:02 AM Deepak Vohra <<a href="mailto:dvohra16@gmail.com" target="_blank">dvohra16@gmail.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"><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"><div dir="ltr" class="gmail_attr">On Fri, Sep 5, 2025 at 7:09 AM Simon Ritter <<a href="mailto:sritter@azul.com" target="_blank">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>
</blockquote></div>
</blockquote></div>