Java – how do I find out if an object is an integer or isa string or isa Boolean?

I have an object that I want to detect, so I can call

if (obj isa Integer)
  put(key,integerval);  
if (obj isa String)
    put(key,stringval);  
if (obj isa Boolean)
    put(key,booleanval);

Solution

You're really close!

if (obj instanceof Integer)
    put(key,integerval);  
if (obj instanceof String)
    put(key,stringval);  
if (obj instanceof Boolean)
    put(key,booleanval);

From JLS 15.20 2:

Look at your usage patterns, but it seems that you may encounter bigger problems than this

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>