How can I check if an object is of type string?
4 Answers
Like this:
Integer myInt = 3;
if (myInt instanceof String)
System.out.println("It's a String!");
else
System.out.println("Not a String :(");
2 Comments
Bananeweizen
This is a bad example, as the String is already declared as String, and not as Object (or other super class of String).
Bananeweizen
I thought more of declaring an Object, because declaring an Integer or a String makes this a compile time decision. Only for a super class of String (which neither Integer nor String are) this is a runtime decision. But anyway, I removed my down vote.
obj instanceof String?