java - 在 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
解决方案
推荐阅读
- python - 从回购错误中实现模型:具有多个元素的数组的真值不明确
- html - 对 HTML 的 rest api 调用有问题
- javascript - 如何根据 EJS 模板中的特定条件隐藏/显示按钮
- c# - 未选中复选框时,它应该处理覆盖或删除 c# Visual Studio 中以前保存的文件
- r - 如何将滑块输入作为 pie() 的标签包含在内
- spring - spring maven简单代码出错
- c - 查找数组中与给定值最接近的数字的函数(在 C 中)
- blazor-server-side - Blazor 服务器组件 - 如何以两种方式绑定输入 html 标记和调用函数 onchange
- angular - 如何在 Angular 9 中覆盖 addEventListener 的单元测试用例
- c# - MediaTypeHeaderValue 类是线程安全的吗?