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
3860 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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template