当在第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发布于 2018-10-10 17:24:43
问题是,尽管它说它被设置为null,但实际上它并不是。
我已经将bug标题更改为: JShell:重新声明的变量应该重置
我将尝试在JDK 12中修复。
第二个问题不是bug,Java不允许不真实的操作符实例--行为与javac完全匹配。
发布于 2018-10-04 10:12:19
https://stackoverflow.com/questions/52611547
复制相似问题