<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)">
<!--[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 Definitions */
@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;}
/* Style Definitions */
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;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:574897837;
mso-list-template-ids:-1935269796;}
@list l0:level1
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level2
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:1.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level3
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:1.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level4
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:2.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level5
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:2.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level6
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:3.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level7
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:3.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level8
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:4.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l0:level9
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:4.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1
{mso-list-id:907770266;
mso-list-type:hybrid;
mso-list-template-ids:-1087065100 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l1:level1
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l1:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l1:level3
{mso-level-number-format:bullet;
mso-level-text:\F0A7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l1:level4
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l1:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l1:level6
{mso-level-number-format:bullet;
mso-level-text:\F0A7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l1:level7
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l1:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l1:level9
{mso-level-number-format:bullet;
mso-level-text:\F0A7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
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]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="purple" style="word-wrap:break-word">
<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 <angelos.bimpoudis@oracle.com><br>
<b>Date: </b>Wednesday, November 8, 2023 at 2:56 PM<br>
<b>To: </b>Ella Ananeva <ella.ananeva@oracle.com>, amber-dev@openjdk.org <amber-dev@openjdk.org><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" align="center" style="text-align:center">
<hr size="0" width="100%" align="center">
</div>
<div id="divRplyFwdMsg">
<p class="MsoNormal"><b><span style="color:black">From:</span></b><span style="color:black"> amber-dev <amber-dev-retn@openjdk.org> on behalf of Ella Ananeva <ella.ananeva@oracle.com><br>
<b>Sent:</b> 08 November 2023 19:18<br>
<b>To:</b> amber-dev@openjdk.org <amber-dev@openjdk.org><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">
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">
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>
</body>
</html>