instanceof Strictly speaking, it is a binary operator in Java, used to test whether an object is an instance of a class. The usage is: (Recommended to learn : java course)
boolean result = obj instanceof Class
where obj is an object, and Class represents a class or an interface. When obj is an object of Class, or its direct or indirect subclass, or its The implementation class of the interface returns true as a result, otherwise it returns false.
Note: The compiler will check whether obj can be converted to the class type on the right. If it cannot be converted, an error will be reported directly. If the type cannot be determined, it will be compiled. It depends on the runtime.
The value of the formula is a boolean
Object sth; bool isString = sth instanceof String;
or
if (sth instanceof String) { // your code }
The above is the detailed content of Java determines whether a variable is a string. For more information, please follow other related articles on the PHP Chinese website!