<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Menlo;
        panose-1:2 11 6 9 3 8 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-ligatures:standardcontextual;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
code
        {mso-style-priority:99;
        font-family:"Courier New";}
p.p1, li.p1, div.p1
        {mso-style-name:p1;
        margin:0in;
        font-size:8.5pt;
        font-family:Menlo;
        color:black;}
span.s1
        {mso-style-name:s1;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        font-family:"Calibri",sans-serif;
        mso-ligatures:none;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p>Hi team,<o:p></o:p></p>
<p>When we match labels in switch with primitive types, the spec defines the behavior for integral types and for floating-point types.
<o:p></o:p></p>
<p><span style="font-family:Symbol">·</span>  <strong><span style="font-family:"Calibri",sans-serif">If
</span></strong><em><b><span style="font-family:"Calibri",sans-serif">T</span></b></em><strong><span style="font-family:"Calibri",sans-serif"> is a primitive type, then a</span></strong> 
<code><span style="font-size:10.0pt">case</span></code> label with a <code><span style="font-size:10.0pt">case</span></code> constant
<em><span style="font-family:"Calibri",sans-serif">c</span></em> applies to a value that is of type
<code><span style="font-size:10.0pt">char</span></code>, <code><span style="font-size:10.0pt">byte</span></code>,
<code><span style="font-size:10.0pt">short</span></code>, <code><span style="font-size:10.0pt">int</span></code>, 
<code><b><span style="font-size:10.0pt">long</span></b></code><strong><span style="font-family:"Calibri",sans-serif">,
</span></strong><code><b><span style="font-size:10.0pt">float</span></b></code><strong><span style="font-family:"Calibri",sans-serif">,
</span></strong><code><b><span style="font-size:10.0pt">double</span></b></code><strong><span style="font-family:"Calibri",sans-serif">,
</span></strong><code><b><span style="font-size:10.0pt">boolean</span></b></code><strong><span style="font-family:"Calibri",sans-serif">,</span></strong>
<code><span style="font-size:10.0pt">String</span></code> or an enum type if the constant
<em><span style="font-family:"Calibri",sans-serif">c</span></em> is equal to the value.</p>
<p>Equality is defined in terms of the <code><span style="font-size:10.0pt">==</span></code> operator
<strong><span style="font-family:"Calibri",sans-serif">(<a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.21">15.21</a>) for the integral types and the boolean type, and in terms of representation equivalence (<a href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Double.html#repEquivalence">java.lang.Double</a>)
 for the floating-point types.</span></strong>  <strong><span style="font-family:"Calibri",sans-serif">If</span></strong> the value is a
<code><span style="font-size:10.0pt">String</span></code>,  equality is defined in terms of the
<code><span style="font-size:10.0pt">equals</span></code> method of class <code><span style="font-size:10.0pt">String</span></code>.</p>
<p><o:p> </o:p></p>
<p>What should be the behavior, though, when we try to match an integral type to a floating-point type?<o:p></o:p></p>
<p>e.g., In this scenario:</p>
<p class="p1"><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">private static void
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#00627A">testSwitch</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">(</span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">float
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">y) {<br>
    </span><span style="font-size:10.0pt;font-family:"Courier New"">System</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:#871094">out</span></i><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.println(y
 == </span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">);     //prints true<br>
    </span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">switch
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">(y) {<br>
        </span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3">case
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1 </span>
<span style="font-size:10.0pt;font-family:"Courier New";color:#080808">-> { </span>
<span style="font-size:10.0pt;font-family:"Courier New"">System</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.</span><i><span style="font-size:10.0pt;font-family:"Courier New";color:#871094">out</span></i><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">.println(y
 == </span><span style="font-size:10.0pt;font-family:"Courier New";color:#1750EB">1</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808">);}//throws a compile-time error:
</span><span class="s1">constant label of type int is not compatible with switch selector type float</span><o:p></o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier New";color:#080808;mso-ligatures:none"><br>
        </span><span style="font-size:10.0pt;font-family:"Courier New";color:#0033B3;mso-ligatures:none">default
</span><span style="font-size:10.0pt;font-family:"Courier New";color:#080808;mso-ligatures:none">-> { }<br>
    }<br>
}<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I would expect the compiler to be able to handle this situation: since int can be converted to float and the equality operator returns true, the label should be chosen.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The opposite situation with narrowing conversion, when we try to convert float to int may be trickier, but shouldn’t it also be accepted?</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thank you,</p>
<p class="MsoNormal">Ella Ananeva</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>