isAssignableFrom(Class): This method is available in the java.lang.Class of Java.
Syntax:
boolean java.lang.Class.isAssignableFrom(Class cls)
This method takes one argument. This method determines if the class or interface represented by this Class object is either the same as, or is a superclass or super interface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false.
If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise, it returns false.
Parameters: One parameter is required for this method.
cls: the Class object to be checked.
Returns: the boolean value indicating whether objects of the type cls can be assigned to objects of this class.
Throws:
1. NullPointerException - if the specified Class parameter is null.
Approach 1: When no exception
Java
package com.Class;public class ClassisAssignableFrom {@SuppressWarnings("unchecked")public static void main(String[] args)throws ClassNotFoundException {@SuppressWarnings("rawtypes")Class class1 = Class.forName("java.lang.String");@SuppressWarnings("rawtypes")Class cls = Class.forName("java.lang.String");System.out.println(class1.isAssignableFrom(cls));}}
Output:
true
Approach 2: NullPointerException
Java
package com.Class;public class ClassisAssignableFrom {@SuppressWarnings("unchecked")public static void main(String[] args)throws ClassNotFoundException {@SuppressWarnings("rawtypes")Class class1 = Class.forName("java.lang.String");@SuppressWarnings("rawtypes")Class cls = null;System.out.println(class1.isAssignableFrom(cls));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.lang.Class.isAssignableFrom(Native Method) at com.Class.ClassisAssignableFrom.main(ClassisAssignableFrom.java:14)
Some other methods of Class
getResource(String): This method finds a resource with a given name.
getResourceAsStream(String): This method finds a resource with a given name.
getSigners(): This method gets the signers of this class.
getSimpleName(): This method returns the simple name of the underlying class as given in the source code.
getSuperclass(): This method returns the Class representing the direct superclass of the entity (class, interface, primitive type, or void) represented by this Class.
getTypeName(): This method returns an informative string for the name of this type.
getTypeParameters(): This method returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.
isAnnotation(): This method returns true if this Class object represents an annotation type.
isAnnotationPresent(Class): This method returns true if an annotation for the specified type is present on this element, else false.
isAnonymousClass(): This method returns true if and only if the underlying class is an anonymous class.
isArray(): This method determines if this Class object represents an array class.
isAssignableFrom(Class): This method determines if the class or interface represented by this Class object is either the same as, or is a superclass or super interface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false.
isEnum(): This method returns true if and only if this class was declared as an enum in the source code.
isHidden(): This method returns true if and only if the underlying class is a hidden class.
isInstance(Object): This method determines if the specified Object is assignment-compatible with the object represented by this Class.
isInterface(): This method determines if this Class object represents an interface type.
isLocalClass(): This method returns true if and only if the underlying class is a local class.
isMemberClass(): This method returns true if and only if the underlying class is a member class.
isNestmateOf(Class): This method determines if the given Class is a nestmate of the class or interface represented by this Class object.
isPrimitive(): This method determines if the specified Class object represents a primitive type.
isRecord(): This method returns true if and only if this class is a record class.
isSealed(): This method returns true if and only if this Class object represents a sealed class or interface.
isSynthetic(): This method returns true if and only if this class has the synthetic modifier bit set.
permittedSubclasses(): This method returns an array containing ClassDesc objects representing all the direct subclasses or direct implementation classes permitted to extend or implement this class or interface if it is sealed.
toGenericString(): This method returns a string describing this Class, including information about modifiers and type parameters.
toString(): This method converts the object to a string. The string representation is the string "class" or "interface", followed by a space, and then by the name of the class in the format returned by getName.
No comments:
Post a Comment