微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个对象,我想检测什么类型,所以我可以打电话
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.

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

原文地址:https://www.jb51.cc/java/125870.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐