How can I know if Object is String type object?

Java

Java Problem Overview


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.

try {
	String myString = (String) object;
	// do stuff here
} catch(Exception e) {
	// it wasn't string, so just continue
}

Java Solutions


Solution 1 - Java

 object instanceof Type

is true if the object is a Type or a subclass of Type

 object.getClass().equals(Type.class) 

is true only if the object is a Type

Solution 2 - Java

Use the instanceof syntax.

Like so:

Object foo = "";

if( foo instanceof String ) {
  // do something String related to foo
}

Solution 3 - Java

Guard your cast with instanceof

String myString;
if (object instanceof String) {
  myString = (String) object;
}

Solution 4 - Java

Either use instanceof or method Class.isAssignableFrom(Class<?> cls).

Solution 5 - Java

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.

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).

Solution 6 - Java

From JDK 14+ which includes JEP 305 we can do Pattern Matching for 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

if (obj instanceof String) {
    String str = (String) obj; // need to declare and cast again the object
    .. str.contains(..) ..
}else{
     str = ....
}

Java 14 enhancements

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.

PS: instanceOf will only match when the object is not null, then only it can be assigned to str.

Solution 7 - Java

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);

Solution 8 - Java

Could you not use typeof(object) to compare against

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnewbieView Question on Stackoverflow
Solution 1 - JavaAndreas DolkView Answer on Stackoverflow
Solution 2 - Javajavamonkey79View Answer on Stackoverflow
Solution 3 - JavamR_fr0gView Answer on Stackoverflow
Solution 4 - JavaVictor SorokinView Answer on Stackoverflow
Solution 5 - JavaMartin GrossView Answer on Stackoverflow
Solution 6 - JavaPlayer_NeoView Answer on Stackoverflow
Solution 7 - JavaPeter LawreyView Answer on Stackoverflow
Solution 8 - JavaPreethaAView Answer on Stackoverflow