Long l1 = null;
Long l2 = Long.getLong("23");
Long l3 = Long.valueOf(23);
System.out.println(l1 instanceof Long); // returns false
System.out.println(l2 instanceof Long); // returns false
System.out.println(l3 instanceof Long); // returns true
Run Code Online (Sandbox Code Playgroud)
我无法理解返回的输出.我期待第二和第三个系统的真正至少.有人能解释一下instanceof的工作原理吗?
Mic*_*rdt 15
这与此无关instanceof.该方法Long.getLong()不解析字符串,它返回具有该名称的系统属性的内容,解释为long.由于没有名称为23的系统属性,因此返回null.你要Long.parseLong()
dfa*_*dfa 11
l1 instanceof Long
因为l1为null,instanceof产生false(由Java languange规范指定)
l2 instanceof Long
这会产生错误,因为您使用了错误的方法getLong:
Determines the long value of the system property with the specified name.
Long.getLong(..)返回系统属性的long值.它会null在您的情况下返回,因为没有名为"23"的系统属性.所以:
null,并且在比较空值时instanceof返回falsejava.lang.Long(你可以通过输出检查l3.getClass())所以true是预期的而不是使用Long.getLong(..),用于Long.parseLong(..)解析String.
| 归档时间: |
|
| 查看次数: |
4521 次 |
| 最近记录: |