<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4"><font face="monospace">Unfortunately the behavior of
instanceof and switch has always been different on this matter;
instanceof evaluates to false when presented with a null, and
switch throws NPE when presented with a null. (They both
dislike nulls, but one is more tolerant than the other.) <br>
<br>
When we added pattern support to switch, we extended this
behavior to support `null` case labels. If a switch has a `case
null`, then when given a null selector, the appropriate case
will be run, rather than the switch completing abruptly with
NPE. This preserves compatibility with existing code, while
enabling null to be treated as "just another value" if desired.
<br>
<br>
So the NPE you are seeing is not coming from the unboxing
operation in `case byte x`, but before we even enter the switch,
because the first thing switch does is evaluate the selector,
and if that succeeds, checks for null, and if null, and no `case
null` present, the switch completes abruptly with NPE. <br>
<br>
</font></font><br>
<div class="moz-cite-prefix">On 11/8/2023 6:08 PM, Ella Ananeva
wrote:<br>
</div>
<blockquote type="cite" cite="mid:SN6PR10MB275237A0D739DC58DFF0C352FBA8A@SN6PR10MB2752.namprd10.prod.outlook.com">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->
<style>@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}@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:"Segoe UI";
panose-1:2 11 5 2 4 2 4 2 2 3;}p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}code
{mso-style-priority:99;
font-family:"Courier New";}p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}p.xmsonormal, li.xmsonormal, div.xmsonormal
{mso-style-name:x_msonormal;
margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}span.EmailStyle25
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
mso-ligatures:none;}div.WordSection1
{page:WordSection1;}ol
{margin-bottom:0in;}ul
{margin-bottom:0in;}</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]-->
<div class="WordSection1">
<p class="MsoNormal">Thank you very much for addressing this
issue, Angelos!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I have another question. What is our
approach to treating scenarios when we use instanceof of null
type with cast?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">Byte
b = null;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Courier New"">If
(!(b instanceof byte)) System.our.println(“Yes”); //prints
“Yes”<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In the spec, <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">A reference conversion may not complete
normally, namely:<o:p></o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l1 level1 lfo2">An unboxing
conversion may raise a
<span style="font-size:10.0pt;font-family:"Courier
New"">NullPointerException</span><o:p></o:p></li>
</ul>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Here, no exception is thrown., so the
behavior is as if we had a null type instanceof with a
reference type in the pattern. But we have a primitive type in
the pattern, so I would assume there should be some conversion
going on? Precisely the scenario described in the assertion?
BTW if I do a similar thing in switch, there will be an
exception:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black">Byte y
</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">null</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">;<br>
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">try
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">{<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">(</span><span style="font-size:10.0pt;font-family:"Courier
New";color:black">y</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">) {<br>
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">case byte
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">x: </span>
<span style="font-size:10.0pt;font-family:"Courier
New";color:black">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(</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#067D17">"converted"</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">);<br>
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">default</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:black">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(</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#067D17">"not converted"</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">);<br>
}<br>
} </span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">catch
</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:black">NullPointerException
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">ex) {<br>
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:black">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(</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#067D17">"Threw exception"</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">);<br>
}<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">This code prints “Threw exception.”<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Shouldn’t the behavior of switch and
instanceof be similar in this situation?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thank you,<o:p></o:p></p>
<p class="MsoNormal">Ella<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div id="mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF
1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="font-size:12.0pt;color:black">From:
</span></b><span style="font-size:12.0pt;color:black">Angelos
Bimpoudis <a class="moz-txt-link-rfc2396E" href="mailto:angelos.bimpoudis@oracle.com"><angelos.bimpoudis@oracle.com></a><br>
<b>Date: </b>Wednesday, November 8, 2023 at 2:56 PM<br>
<b>To: </b>Ella Ananeva
<a class="moz-txt-link-rfc2396E" href="mailto:ella.ananeva@oracle.com"><ella.ananeva@oracle.com></a>, <a class="moz-txt-link-abbreviated" href="mailto:amber-dev@openjdk.org">amber-dev@openjdk.org</a>
<a class="moz-txt-link-rfc2396E" href="mailto:amber-dev@openjdk.org"><amber-dev@openjdk.org></a><br>
<b>Subject: </b>Re: instanceof with primitive type<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe
UI",sans-serif;color:black">Hello Ella,
<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe
UI",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe
UI",sans-serif;color:black">Indeed. The bullet
you showed is now deleted from both. The line you
have put in bold, replaces the other two.
<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe
UI",sans-serif;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe
UI",sans-serif;color:black">Thanks for bringing
this up.<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span style="font-family:"Segoe
UI",sans-serif;color:black">Aggelos<o:p></o:p></span></p>
</div>
<div class="MsoNormal" style="text-align:center" align="center">
<hr width="100%" size="0" align="center">
</div>
<div id="divRplyFwdMsg">
<p class="MsoNormal"><b><span style="color:black">From:</span></b><span style="color:black"> amber-dev
<a class="moz-txt-link-rfc2396E" href="mailto:amber-dev-retn@openjdk.org"><amber-dev-retn@openjdk.org></a> on behalf of Ella
Ananeva <a class="moz-txt-link-rfc2396E" href="mailto:ella.ananeva@oracle.com"><ella.ananeva@oracle.com></a><br>
<b>Sent:</b> 08 November 2023 19:18<br>
<b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:amber-dev@openjdk.org">amber-dev@openjdk.org</a>
<a class="moz-txt-link-rfc2396E" href="mailto:amber-dev@openjdk.org"><amber-dev@openjdk.org></a><br>
<b>Subject:</b> instanceof with primitive type</span>
<o:p></o:p></p>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="xmsonormal">Hi team,<o:p></o:p></p>
<p class="xmsonormal">Reading <a href="https://cr.openjdk.org/~abimpoudis/instanceof/jep455-20231030/specs/instanceof-jls.html#jls-15.20.2" moz-do-not-send="true">
JEP 455 spec</a>, I see this:<o:p></o:p></p>
<p><strong><span style="font-family:"Calibri",sans-serif">The
type of the expression
</span></strong><em><b><span style="font-family:"Calibri",sans-serif">RelationalExpression</span></b></em><strong><span style="font-family:"Calibri",sans-serif">
can be a reference type, a primitive type</span></strong>
<strong><span style="font-family:"Calibri",sans-serif">or
the null type.</span></strong><o:p></o:p></p>
<p class="xmsonormal">And then, later:<o:p></o:p></p>
<p>The following rules apply when <code><span style="font-size:10.0pt">instanceof</span></code>
is the pattern match operator:<o:p></o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="xmsonormal" style="mso-list:l0 level1 lfo1">The
type of the expression
<em><span style="font-family:"Calibri",sans-serif">RelationalExpression</span></em>
must be a reference type or the null type, or a
compile-time error occurs.<o:p></o:p></li>
</ul>
<p class="xmsonormal"> <o:p></o:p></p>
<p class="xmsonormal">But I would expect that primitive
types in patterns should be good, too. And in the
freshest version of
<a href="https://mach5.us.oracle.com/mdash/buildIds/2023-11-06-1633093.angelos.bimpoudis.dev" moz-do-not-send="true">
JEP_455 JDK</a>, both examples compile and print
“Yes”:<o:p></o:p></p>
<p class="xmsonormal"> <o:p></o:p></p>
<p class="xmsonormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:black">Integer x
</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:#1750EB">5</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">;<br>
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">if </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:black">x
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">instanceof int
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">y) </span>
<span style="font-size:10.0pt;font-family:"Courier
New";color:black">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(</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#067D17">"Yes"</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">);</span><o:p></o:p></p>
<p class="xmsonormal"> <o:p></o:p></p>
<p class="xmsonormal" style="background:white"><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">int
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:black">x </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:#1750EB">5</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">;<br>
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">if </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:black">x
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#0033B3">instanceof
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:black">Integer
</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">y) </span>
<span style="font-size:10.0pt;font-family:"Courier
New";color:black">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(</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#067D17">"Yes"</span><span style="font-size:10.0pt;font-family:"Courier
New";color:#080808">);</span><o:p></o:p></p>
<p class="xmsonormal"> <o:p></o:p></p>
<p class="xmsonormal">Shouldn’t the spec be updated?<o:p></o:p></p>
<p class="xmsonormal"> <o:p></o:p></p>
<p class="xmsonormal">Thank you,<o:p></o:p></p>
<p class="xmsonormal">Ella Ananeva<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<br>
</body>
</html>