This shot discusses how to determine if the object is an array in Java.
Class
class in JavaJava has a class named Class
in java.lang
package. This class is used for reflection purpose.
Every object in Java belongs to a class. The metadata about the class is captured in the Class
object. The metadata can be the class’s constructors, the class’s methods, the fields of the class, and so on.
Therefore, every object belongs to a class and has a Class
object. The Class
object of any given object can be obtained via the getClass()
method.
isInterface()
methodThe isArray()
method of the Class
class is used to check whether a class is an interface or not. This method returns true
if the given class is an interface. Otherwise, the method returns false
, indicating that the given class is not an interface.
public boolean interface();
public class Main {public static void main(String[] args) {String stringObject = "educative";System.out.printf("Is %s an interface - %s", stringObject, stringObject.getClass().isInterface());System.out.println();Class<?> interfaceClass = Iterable.class;System.out.printf("Is %s an interface - %s", interfaceClass, interfaceClass.isInterface());}}
stringObject
.isInterface()
method to check if the stringObject
belongs to an interface.Class
object for the Iterable
interface, and store it in interfaceClass
.isInterface()
method to check if the interfaceClass
belongs to an interface.