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

Java determines whether an object is a string

尚
Original
2019-11-23 09:35:2211131browse

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

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));
 }
}

When we run the program, the output will be:

是name的String的实例:true
age是Integer的实例:true

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!

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