Home  >  Article  >  Java  >  Java determines whether a variable is a string

Java determines whether a variable is a string

(*-*)浩
(*-*)浩Original
2019-11-19 13:38:173773browse

Java determines whether a variable is a string

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn