Home Java JavaBase Java determines whether an object is a string

Java determines whether an object is a string

Nov 23, 2019 am 09:35 AM
java

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles