如何知道Object是否为String类型对象?
16lz
2021-01-22
I have to know if Object
is String or any other class type, how can I do it? Currently I do it like below, but its not very good coding.
我必须知道Object是String还是任何其他类类型,我该怎么办呢?目前我这样做,如下,但它不是很好的编码。
try {
String myString = (String) object;
// do stuff here
} catch(Exception e) {
// it wasn't string, so just continue
}
7 个解决方案
#1
71
object instanceof Type
is true
if the object is a Type
or a subclass of Type
如果对象是Type的Type或子类,则为true
object.getClass().equals(Type.class)
is true
only if the object is a Type
仅当对象是Type时才为true
更多相关文章
- Java:如何创建特定父类型的集合而不是其子类型?
- javascript(六)js事件绑定浏览器兼容解决方案 attachEvent addEve
- Java-子类和父类的几种关系
- java中多线程安全问题产生&解决方案——同步方法