Is there a way to do this in java?
If variable is a String do one operation, otherwise perform another operation.
For example, given variable a
if a is a string
close program
else
continue program
I’m looking for the code that checks to see if the variable is a String or not.
instanceofoperator is what you use in Java when you want to test whether the given object is of a certain type.The use of such type-checks is discouraged in OOP. You should rather try to solve your problem by employing subtype polymorphism (or inheritance, if you will). (If you describe your specific problem, we could assist you with it.)
An interesting factoid: Scala, another statically typed language on JVM, has a following construct that lets you match over type and cast to that type in one clause: