Close

Java Reflection - Class.isAssignableFrom() Examples

Code Snippets Java Reflection Java Java API 


Class:

java.lang.Class

java.lang.Objectjava.lang.Objectjava.lang.Classjava.lang.Classjava.io.SerializableSerializablejava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.TypeTypejava.lang.reflect.AnnotatedElementAnnotatedElementLogicBig

Method:

public boolean isAssignableFrom(Class<?> cls)
Parameters:
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

Examples


BigInteger extends Number:

package com.logicbig.example.clazz;

import java.math.BigInteger;

public class IsAssignableFromExample {

public static void main(String... args) {
Class<BigInteger> bigIntegerClass = BigInteger.class;
Class<Number> numberClass = Number.class;

boolean b = numberClass.isAssignableFrom(bigIntegerClass);
System.out.println("Number isAssignableFrom BigInteger: " + b);

b = bigIntegerClass.isAssignableFrom(numberClass);
System.out.println("BigInteger isAssignableFrom Number: " + b);
}
}

Output

Number isAssignableFrom BigInteger: true
BigInteger isAssignableFrom Number: false




String implements CharSequence:

package com.logicbig.example.clazz;

public class IsAssignableFromExample2 {

public static void main(String... args) {
Class<String> stringClass = String.class;
Class<CharSequence> charSequenceClass = CharSequence.class;

boolean b = charSequenceClass.isAssignableFrom(stringClass);
System.out.println("CharSequence isAssignableFrom String: " + b);

b = stringClass.isAssignableFrom(charSequenceClass);
System.out.println("String isAssignableFrom CharSequence: " + b);
}
}

Output

CharSequence isAssignableFrom String: true
String isAssignableFrom CharSequence: false




The interface ScheduledFuture extends Future:

package com.logicbig.example.clazz;

import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;

public class IsAssignableFromExample3 {

public static void main(String... args) {
Class<ScheduledFuture> scheduledFutureClass = ScheduledFuture.class;
Class<Future> futureClass = Future.class;

boolean b = futureClass.isAssignableFrom(scheduledFutureClass);
System.out.println("Future isAssignableFrom ScheduledFuture: " + b);

b = scheduledFutureClass.isAssignableFrom(futureClass);
System.out.println("ScheduledFuture isAssignableFrom Future: " + b);
}
}

Output

Future isAssignableFrom ScheduledFuture: true
ScheduledFuture isAssignableFrom Future: false




package com.logicbig.example.clazz;

import java.math.BigDecimal;

public class IsAssignableFromExample4 {

public static void main(String... args) {
Object object = new BigDecimal("100");
Number num = safeCastTo(object, Number.class);
System.out.println(num);
}

public static <T> T safeCastTo(Object obj, Class<T> to) {
if (obj != null) {
Class<?> c = obj.getClass();
if (to.isAssignableFrom(c)) {
return to.cast(obj);
}
}
return null;
}
}

Output

100

Primitives:

package com.logicbig.example.clazz;

public class IsAssignableFromExample5 {

public static void main(String... args) {
boolean b = int.class.isAssignableFrom(byte.class);
System.out.println("int isAssignableFrom byte: " + b);

b = byte.class.isAssignableFrom(int.class);
System.out.println("byte isAssignableFrom int: " + b);

b = int.class.isAssignableFrom(int.class);
System.out.println("int isAssignableFrom int: " + b);

b = Object.class.isAssignableFrom(byte.class);
System.out.println("Object isAssignableFrom int: " + b);
}
}

Output

int isAssignableFrom byte: false
byte isAssignableFrom int: false
int isAssignableFrom int: true
Object isAssignableFrom int: false




Primitives with their wrappers do not work.

package com.logicbig.example.clazz;

public class IsAssignableFromExample6 {

public static void main(String... args) {
boolean b = int.class.isAssignableFrom(Integer.class);
System.out.println("int isAssignableFrom Integer: " + b);

b = Integer.class.isAssignableFrom(int.class);
System.out.println("Integer isAssignableFrom int: " + b);
}
}

Output

int isAssignableFrom Integer: false
Integer isAssignableFrom int: false
Check out this example too.




Arrays:

package com.logicbig.example.clazz;

public class IsAssignableFromExample7 {

public static void main(String... args) {
//primitives
boolean b = int[].class.isAssignableFrom(byte[].class);
System.out.println("int[] isAssignableFrom byte[]: " + b);

b = byte[].class.isAssignableFrom(int[].class);
System.out.println("byte[] isAssignableFrom int[]: " + b);

b = int[].class.isAssignableFrom(int[].class);
System.out.println("int[] isAssignableFrom int[]: " + b);

b = Object[].class.isAssignableFrom(int[].class);
System.out.println("Object[] isAssignableFrom int[]: " + b);

b = Object.class.isAssignableFrom(int[].class);
System.out.println("Object isAssignableFrom int[]: " + b);

//objects
Integer[].class.isAssignableFrom(Byte[].class);
System.out.println("Integer[] isAssignableFrom Byte[]: " + b);

b = Byte[].class.isAssignableFrom(Integer[].class);
System.out.println("Byte[] isAssignableFrom Integer[]: " + b);

b = Integer[].class.isAssignableFrom(Integer[].class);
System.out.println("Integer[] isAssignableFrom Integer[]: " + b);

b = Object[].class.isAssignableFrom(Integer[].class);
System.out.println("Object[] isAssignableFrom Integer[]: " + b);

b = Object.class.isAssignableFrom(Integer[].class);
System.out.println("Object isAssignableFrom Integer[]: " + b);
}
}

Output

int[] isAssignableFrom byte[]: false
byte[] isAssignableFrom int[]: false
int[] isAssignableFrom int[]: true
Object[] isAssignableFrom int[]: false
Object isAssignableFrom int[]: true
Integer[] isAssignableFrom Byte[]: true
Byte[] isAssignableFrom Integer[]: false
Integer[] isAssignableFrom Integer[]: true
Object[] isAssignableFrom Integer[]: true
Object isAssignableFrom Integer[]: true




See Also