instanceof operator only be used for object reference variables , inspect the left than to the right is the test object is an instance of the class or interface . If the measured object is a null value , the test result is always false.
vividly : own instance or subclass instance instanceof own class returns true
Example : String s = new String ("javaisland");
System. out.println (s instanceof String); / / true
Class class isInstance (Object obj) method , obj is the object to be tested , if obj is to call this method an instance of class or interface , return true. This method is the dynamic equivalent instanceof operator .
vividly : own class . class.isInstance ( itself an instance or subclass instance ) returns true
Example : String s = new String ("javaisland");
; System.out.println (String.class.isInstance (s)); / / true
Class class isAssignableFrom (Class cls) method , if you call this method class or interface with parameters cls class or interface represented by the same or a parameter cls class or interface represented by the parent class , it returns true.
vividly : own class . class.isAssignableFrom ( own class or sub-class . class) returns true
Example : System.out.println (ArrayList.class.isAssignableFrom (Object.class)); / / false
System.out.println (Object.class.isAssignableFrom (ArrayList.class)); / / true
2013年7月30日星期二
instanceof, isinstance, isAssignableFrom distinction
订阅:
博文评论 (Atom)
没有评论:
发表评论