<html><head>
<style id="css_styles">
blockquote.cite { margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc }
blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; }
a img { border: 0px; }
li[style='text-align: center;'], li[style='text-align: center; '], li[style='text-align: right;'], li[style='text-align: right; '] { list-style-position: inside;}
body { font-family: 'Segoe UI'; font-size: 12pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; padding-left: 0.3em; }
</style>
</head>
<body><div><span>I came across a bit of a curious way to check if something is not an instance of something, and I noticed it doesn't play along with the new instanceof construct that allows you to assign it to a local variable immediately:</span></div><div><span><br /></span></div><div>
public static void main(String[] args) {
</div><div> Number n = 1.0;
</div><div><br /></div><div> if (n instanceof Double d == false) {
</div><div> System.out.println("It was not a double");
</div><div><br /></div><div> return;
</div><div> }
</div><div><br /></div><div> System.out.println("It was a double: " + d);
// compile error</div><div> }</div><div><br /></div><div>In the above snippet, the variable "d", despite being accepted, is not in scope anywhere.</div><div><br /></div><div>Changing the check to " !(n instanceof Double d)" works as expected.</div><div><br /></div><div>Is this something that was missed, or am I misunderstanding why this can't work?</div><div><br /></div><div>--John</div><div><br /></div></body></html>