java中instanceof和getClass()的区别
对象 instanceof 类名,表示 对象是类名的实例,或者是其子类的实例,返回 true,否则返回 false。
对象.getClass() == 类名.class ,表示 只有对象是该类的实例,才返回 true
class A { }
class B extends A { }
Object o1 = new A();
Object o2 = new B();
o1 instanceof A => true
o1 instanceof B => false
o2 instanceof A => true // <================ HERE
o2 instanceof B => true
o1.getClass().equals(A.class) => true
o1.getClass().equals(B.class) => false
o2.getClass().equals(A.class) => false // <===============HERE
o2.getClass().equals(B.class) => true
getClass() will be useful when you want to make sure your instance is NOT a subclass of the class you are comparing with.
例子:
package a_start._11_instanceof; class Base{ } class Derived extends Base{ } public class TestInstanceof { public static void main(String[] args) { Derived d = new Derived(); boolean bRet; bRet = d instanceof Derived;//true bRet = d instanceof Base;//true bRet = d.getClass() == Derived.class;//true bRet = d.getClass().equals(Derived.class);//true // bRet = d.getClass() == Base.class;// 出错 Incompatible operand types ,可以强转(Object)就不报错了 bRet = d.getClass().equals(Base.class);//false //boolean java.lang.Class.isInstance(Object obj) // true if obj is an instance of this class System.out.println(Derived.class.isInstance(d));//true System.out.println(Base.class.isInstance(d));//true //扩展 System.out.println(String.class instanceof Class);//true 对象String.class 是 Class的实例 System.out.println("test" instanceof String);//true System.out.println("\"test\".getClass().isInstance(Object.class):"+("test".getClass().isInstance(Object.class))); System.out.println("\"test\".getClass().isInstance(String.class):"+("test".getClass().isInstance(String.class))); System.out.println("String.class.isInstance(Object.class):"+(String.class.isInstance(Object.class))); System.out.println("String.class.isInstance(String.class):"+(String.class.isInstance(String.class))); System.out.println("\"test\".getClass().getClass().isInstance(Object.class):" +("test".getClass().getClass().isInstance(Object.class))); System.out.println("\"test\".getClass().getClass().isInstance(String.class):" +("test".getClass().getClass().isInstance(String.class))); System.out.println("String.class.getClass().isInstance(Object.class):" +(String.class.getClass().isInstance(Object.class))); System.out.println("String.class.getClass().isInstance(String.class):" +(String.class.getClass().isInstance(String.class))); /* "test".getClass().isInstance(Object.class):false // "test".getClass() 等价于 String.class "test".getClass().isInstance(String.class):false //表示 对象String.class 是String的实例吗,显然不是 String.class 是Class的实例 String.class.isInstance(Object.class):false String.class.isInstance(String.class):false "test".getClass().getClass().isInstance(Object.class):true "test".getClass().getClass().isInstance(String.class):true String.class.getClass().isInstance(Object.class):true // Class<? extends Class> String.class.getClass().isInstance(String.class):true // 表示 String.class 是 Class<? extends String> 实例, 是Class<? extends Class>的子类实例 */ } }
使用,重写equals
class Person{ private String id; private String name; //重写equals()方法,提供自定义的相等标准 public boolean equals(Object obj){ if(this == obj)//若为同一个对象 return true; //只有当obj是Person对象 if(null != obj && obj.getClass() == Person.class){ Person p = (Person)obj; if(id.equals(obj).getId() && ...){ return true; } } return false; } }
判断是否为空
private static Boolean IsNullOrEmpty(Object obj) { if(null == obj) return false; if(obj instanceof String) { // 是字符串 ,也可以用 String.class.isInstance(obj) return ((String)obj).trim().length() == 0; } if(obj instanceof Object[]) {//是数组(不包括基本类型数组,像 int[]), 或用 obj.getClass().isArray() return ((Object[])obj).length == 0; } if(obj instanceof java.util.Collection) { //是集合(包括java.util.List,Set, Queue, 等子类 ) return 0 == ((java.util.Collection)obj).size(); } if(obj instanceof java.util.Map) { // 是map return 0 == ((java.util.Map)obj).size(); } return false; }
另外:[Java] Java instanceof 和 Class.isInstance()区别与应用
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。