java - 关于 instanceof 工作的问题

标签 java instanceof

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

我无法理解返回的输出。我期待 true atleast 对于 2nd 和 3rd syso's。谁能解释一下 instanceof 是如何工作的?

最佳答案

这与instanceof无关。 Long.getLong() 方法不解析字符串,它返回具有该名称的系统属性的内容,解释为 long。由于没有名称为 23 的系统属性,因此它返回 null。你想要 Long.parseLong()

关于java - 关于 instanceof 工作的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2153953/

相关文章:

javascript - 如何摆脱javascript中的instanceof

Java instanceof 内部类无法转换错误

java - 在 Stream 中为 Instanceof 应用模式匹配

java - 从另一个程序触发java程序会使用相同的JVM还是会使用不同的JVM?

java - 哪种 m2eclipse 原型(prototype)可用于基于 JBoss/Hibernate 的项目?

Java 对象与 null 情况比较?

kotlin - 在 Kotlin 中获取变量的类型

java - 通过 Id 获取线程

java - 从 jndi 注入(inject)属性

java - 为什么要使用 instanceof 运算符?