Home  >  Article  >  Java  >  Java determines whether an object is an array

Java determines whether an object is an array

尚
Original
2019-11-20 13:38:123417browse

Java determines whether an object is an array

Determine whether the object is an array:

public static void main(String[] args) {
        String[] a = ["1","2"];

        if(a instanceof String[]){
            System.out.println("ss")
        }

        if(a.getClass().isArray()){
            System.out.println("yy")
        }
    }

The first method: instanceof

The instanceof operator in java is used to point out at runtime Whether the 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.

Second approach: Class class isArray()

isArray() method is used to determine whether an object is an array.

Returns true if the object is an array, otherwise returns false.

Syntax:

Array.isArray(obj)

Parameters: obj Required, the object to be judged.​

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

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