public boolean IsAssignableFrom (Class
if (List.class.isAssignableFrom(ArrayList.class)) { System.out.println("list is assignable from arraylist");}if (ArrayList.class.isAssignableFrom(List.class)) { System.out.println("arraylist is assignable from list");}
The result of the output is: list was assignable from ArrayList
Original URL: http://blog.csdn.net/csdn1234/article/details/2563597
Instanceof is used to determine whether an instance of an object is an instance of a class or interface or its child-class subinterfaces.
The format is: oo instanceof TypeName
The first parameter is the object instance name, and the second argument is a specific class name or interface name, such as String,inputstream.
The instance code is as follows:
String"";System.out.println("srt instanceof Object="instanceofObject));ObjectnewObject();System.out.println("oo instanceof Object="instanceofObject));
Results:
SRT instanceof Object=true
OO instanceof object=true
I believe that through the above two examples of basic learning IsAssignableFrom and instanceof usage, basic understanding of their differences!!
<U> Class<? extends U> asSubclass(Class<U> clazz)
This is a method in Java.lang.Class that converts the class object that invokes the method to a subclass of the class object represented by the Clazz parameter. For example
list< String ; strlist = new Arraylist<string (); Class<? extends list > strlist_cast = Strlist . getclass (). assubclass (list .) class );
The above code converts the class object obtained by Strlist.getclass () to
Class<? extends List>
, it doesn't seem to make sense, because we know that the class object that Strlist.getclass () gets is ArrayList, It is of course a subclass of List.class; In some cases, we do not know the type of a class object, typically Class.forName () Gets the class object: The return type of Class.forName () is class< But this is obviously too broad, assuming we need a class object of type List.class, but the arguments we pass to Class.forName are unknown (possibly "java.lang.String" or " Java.util.ArrayList "), then we can use the Assubclass () method, as follows:
Class.forName("xxx.xxx.xxx").asSubclass(List.class).newInstance();
When Xxx.xxx.xxx is a subclass of list, it executes normally, when not a subclass of list, throws ClassCastException, then we can do some other processing, if we look at Class.assubclass () in the JDK in the reference, You'll find that there are a lot of this usage.
Assubclass is used to narrow the scope of an unknown class type, while instanceof is used to determine whether an object reference is a subclass of a superclass or an implementation class for an interface, and if attempting to use instanceof for class type judgment can cause a compilation error.
// both will cause compliration error System.out.println(IFaceImpl.class instanceof IFace); System.out.println(IFaceImplExt.class instanceof IFace.class);
This method is often useful when passing parameters to APIs that are strictly restricted to certain parameters, in order to avoid compiling warnings.
The differences between IsAssignableFrom, instanceof and Assubclass in class methods and examples