Java 我怎么知道 Object 是否是 String 类型的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4344871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I know if Object is String type object?
提问by newbie
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
}
采纳答案by Andreas Dolk
object instanceof Type
is true
if the object is a Type
or a subclass of Type
是true
如果对象是一个Type
或一个子类Type
object.getClass().equals(Type.class)
is true
only if the object is a Type
是true
仅当对象是一个Type
回答by javamonkey79
Use the instanceof
syntax.
使用instanceof
语法。
Like so:
像这样:
Object foo = "";
if( foo instanceof String ) {
// do something String related to foo
}
回答by Victor Sorokin
Either use instanceof
or method Class.isAssignableFrom(Class<?> cls)
.
无论是使用instanceof
还是方法Class.isAssignableFrom(Class<?> cls)
。
回答by mR_fr0g
Guard your cast with instanceof
保护你的演员阵容 instanceof
String myString;
if (object instanceof String) {
myString = (String) object;
}
回答by Peter Lawrey
Its possible you don't need to know depending on what you are doing with it.
您可能不需要知道这取决于您正在做什么。
String myString = object.toString();
or if object can be null
或者如果对象可以为空
String myString = String.valueOf(object);
回答by Martin Gross
javamonkey79 is right. But don't forget what you might want to do (e.g. try something else or notify someone) if object is not an instance of String.
javamonkey79 是对的。但是不要忘记如果 object 不是 String 的实例,您可能想要做什么(例如尝试其他事情或通知某人)。
String myString;
if (object instanceof String) {
myString = (String) object;
} else {
// do something else
}
BTW: If you use ClassCastException instead of Exception in your code above, you can be sure that you will catch the exception caused by casting object to String. And not any other exceptions caused by other code (e.g. NullPointerExceptions).
顺便说一句:如果您在上面的代码中使用 ClassCastException 而不是 Exception,则可以确保您将捕获由将 object 转换为 String 引起的异常。而不是由其他代码引起的任何其他异常(例如 NullPointerExceptions)。
回答by PreethaA
Could you not use typeof(object) to compare against
你能不能用 typeof(object) 来比较
回答by Bhanu Hoysala
From JDK 14+ which includes JEP 305we can do Pattern Matching for instanceof
从包含JEP 305 的JDK 14+ 开始,我们可以为instanceof
Patterns basically test that a value has a certain type, and can extract information from the value when it has the matching type.
模式基本上测试一个值是否具有某种类型,并且当它具有匹配类型时可以从该值中提取信息。
Before Java 14
在 Java 14 之前
if (obj instanceof String) {
String str = (String) obj; // need to declare and cast again the object
.. str.contains(..) ..
}else{
str = ....
}
Java 14 enhancements
Java 14 增强功能
if (!(obj instanceof String str)) {
.. str.contains(..) .. // no need to declare str object again with casting
} else {
.. str....
}
We can also combine the type check and other conditions together
我们还可以将类型检查和其他条件结合在一起
if (obj instanceof String str && str.length() > 4) {.. str.contains(..) ..}
The use of pattern matching in instanceof
should reduce the overall number of explicit casts in Java programs.
使用模式匹配instanceof
应该会减少 Java 程序中显式转换的总数。
PS: instanceOf
will only match when the object is not null, then only it can be assigned to str
.
PS:instanceOf
只有当对象不为空时才会匹配,然后只能将其分配给str
.