Home > Java > JavaBase > Java determines whether an object is a string

Java determines whether an object is a string

Release: 2019-11-23 09:35:22
Original
11345 people have browsed it

Java determines whether an object is a string

The instanceof operator in java is used to indicate at runtime whether an object is an instance of a specific class. instanceof returns a Boolean value indicating whether the object is an instance of this specific class or a subclass of it.

Usage:

result = object instanceof class
Copy after login

Parameters:

Result: Boolean type.

Object: required. Any object expression.

Class: required. Any defined object class.

Description:

If object is an instance of class, the instanceof operator returns true. Returns false if object is not an instance of the specified class, or if object is null.

Example:

class Main {
 public static void main (String[] args) {
   String name = "Programiz";
   Integer age = 22;

   System.out.println("Is name an instance of String: "+ (name instanceof String));
   System.out.println("Is age an instance of Integer: "+ (age instanceof Integer));
 }
}
Copy after login

When we run the program, the output will be:

是name的String的实例:true
age是Integer的实例:true
Copy after login

In the above example, we have created an object in the name String type and an Integer type of the age of another object. We then use the instanceof operator to check if the name is of type String and the age is of type Integer.

For more java knowledge, please pay attention to java basic tutorial.

The above is the detailed content of Java determines whether an object 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