首页 > 解决方案 > 在 jshell-11 中,为什么重置为 null 的重新声明的引用变量仍然具有类型?

问题描述

在第 33 行重新声明 Integer 'a' 时,为什么 jshell 将引用变量显示为 Integer 的实例(请参阅第 38 和 39 行)?重新声明后,第 34 行显示 'a' 设置为 null。当 'a' 在第 6 行声明但未给定值,或在第 22 行重置为 null 时,'a' 不被视为 Integer 的实例。我希望当引用变量被重新声明时,因为它的值为空,它不会是一个类型的实例;然而,事实并非如此。

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

标签: javainstanceofjshelljava-11

解决方案


我已将其作为错误提出并已被接受。

https://bugs.openjdk.java.net/browse/JDK-8211694

好地方。


推荐阅读