Home > Java > JavaBase > body text

Java determines whether an object is an array

Release: 2019-11-20 13:38:12
Original
3407 people have browsed it

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")
        }
    }
Copy after login

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
Copy after login

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)
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!