加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何找出对象是整数还是isa字符串或isa布尔值?

发布时间:2020-12-14 16:18:01 所属栏目:Java 来源:网络整理
导读:我有一个对象,我想检测什么类型,所以我可以打电话 if (obj isa Integer) put(key,integerval); if (obj isa String) put(key,stringval); if (obj isa Boolean) put(key,booleanval); 解决方法 你真的很亲密! if (obj instanceof Integer) put(key,integerv
我有一个对象,我想检测什么类型,所以我可以打电话
if (obj isa Integer)
  put(key,integerval);  
if (obj isa String)
    put(key,stringval);  
if (obj isa Boolean)
    put(key,booleanval);

解决方法

你真的很亲密!
if (obj instanceof Integer)
    put(key,integerval);  
if (obj instanceof String)
    put(key,stringval);  
if (obj instanceof Boolean)
    put(key,booleanval);

从JLS 15.20.2:

RelationalExpression instanceof ReferenceType

At run time,the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

看看你的使用模式,但是,看起来你可能会遇到比这更大的问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读