[英]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.