[英]In jshell-11, why does a redeclared reference variable that resets to null still have a type?
当在第33行重新声明Integer'a'时,为什么jshell将引用变量显示为Integer的实例(参考第38和39行)? 重新声明后,第34行显示'a'设置为null。 当'a'在第6行声明但未给出值时,或在第22行中重置为null时,'a'不被视为Integer的实例。 我希望当引用变量被重新声明时,因为它的值为null,所以它不是一个类型的实例; 然而,事实并非如此。
01: java-lava:~ cafedude$ jshell
02: | Welcome to JShell -- Version 11
03: | For an introduction type: /help intro
04:
05: jshell> Integer a;
06: a ==> null
07: | created variable a : Integer
08:
09: jshell> a instanceof Integer;
10: $2 ==> false
11: | created scratch variable $2 : boolean
12:
13: jshell> a = 1;
14: a ==> 1
15: | assigned to a : Integer
16:
17: jshell> a instanceof Integer;
18: $4 ==> true
19: | created scratch variable $4 : boolean
20:
21: jshell> a = null;
22: a ==> null
23: | assigned to a : Integer
24:
25: jshell> a instanceof Integer;
26: $6 ==> false
27: | created scratch variable $6 : boolean
28:
29: jshell> a = 1;
30: a ==> 1
31: | assigned to a : Integer
32:
33: jshell> Integer a;
34: a ==> null
35: | modified variable a : Integer
36: | update overwrote variable a : Integer
37:
38: jshell> a instanceof Integer;
39: $9 ==> true
40: | created scratch variable $9 : boolean
问题是,虽然它说它被设置为null,但实际上并非如此。 有关详细信息,请参阅bug中添加的注释。
我已经将bug标题更改为:JShell:应该重置Redeclared变量
我将尝试修复JDK 12。
第二个问题不是bug,Java不允许不能为true的instanceof运算符 - 行为完全匹配javac。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.