Home > Java > Javagetting Started > Java determines whether a variable is a string

Java determines whether a variable is a string

(*-*)浩
Release: 2019-11-19 13:38:17
Original
3807 people have browsed it

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
Copy after login

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;
Copy after login

or

if (sth instanceof String) {
    // your code
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template