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!