instanceof、isInstance、==、equals判断Class是否相等

instanceof 关键字 和 isInstance的用法相同

现在有一个类Base

public class Base {

}

还有一个类Derived继承于Base

public class Derived extends Base{

}

再写一个类判断上述条件下两者是否相等

public class FamilyVsExactType {

	static void test(Object x) {
		System.out.println("Testing x of type=" + x.getClass());
		System.out.println("x instanceof Base=" + (x instanceof Base));
		System.out.println("x instanceof Derived=" + (x instanceof Derived));
		System.out.println("Base.isInstance(x)=" + Base.class.isInstance(x));
		System.out.println("Derived.isInstance(x)=" + Derived.class.isInstance(x));
		System.out.println("x.getClass() == Base.class=" + (x.getClass() == Base.class));
		System.out.println("x.getClass() == Derived.class=" + (x.getClass() == Derived.class));
		System.out.println("x.getClass.equals(Base.class)=" + (x.getClass().equals(Base.class)));
		System.out.println("x.getClass.equals(Derived.class)=" + (x.getClass().equals(Derived.class)));
		
	}
	
	public static void main(String[] args) {
		test(new Base());
		test(new Derived());
	}
}

== 和 equals比较的实际的Class类,没有考虑继承,instanceof 和 isInstance考虑继承

输出结果:

Testing x of type=class com.my.rest.six.Base

x instanceof Base=true

x instanceof Derived=false

Base.isInstance(x)=true

Derived.isInstance(x)=false

x.getClass() == Base.class=true

x.getClass() == Derived.class=false

x.getClass.equals(Base.class)=true

x.getClass.equals(Derived.class)=false

Testing x of type=class com.my.rest.six.Derived

x instanceof Base=true

x instanceof Derived=true

Base.isInstance(x)=true

Derived.isInstance(x)=true

x.getClass() == Base.class=false

x.getClass() == Derived.class=true

x.getClass.equals(Base.class)=false

x.getClass.equals(Derived.class)=true


引用自 《thinking in Java》第14章


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,== 和 equals 都用于比较两个对象的值,但它们的实现方式有所不同。 == 操作符用于比较两个对象的引用是否相同,即比较两个对象是否指向同一个内存地址。如果两个引用指向同一个对象,则它们是相等的;否则,它们是不相等的。例如: ``` String str1 = "hello"; String str2 = "hello"; String str3 = new String("hello"); System.out.println(str1 == str2); // true System.out.println(str1 == str3); // false ``` 在上面的代码中,str1 和 str2 指向同一个字符串对象,因此 str1 == str2 的结果为 true。而 str3 是通过 new 关键字创建的一个新的字符串对象,它与 str1 和 str2 的引用不同,因此 str1 == str3 的结果为 false。 equals 方法用于比较两个对象的内容是否相等,即比较两个对象的值是否相等。通常情况下,我们需要自己重写 equals 方法来定义相等的规则。例如: ``` class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof Person)) { return false; } Person other = (Person) obj; return this.name.equals(other.name) && this.age == other.age; } } Person p1 = new Person("Tom", 20); Person p2 = new Person("Tom", 20); System.out.println(p1.equals(p2)); // true ``` 在上面的代码中,我们重写了 Person 类的 equals 方法,定义了当两个 Person 对象的 name 和 age 属性都相等时,它们是相等的。因此,p1 和 p2 的 equals 方法返回 true。 总之,== 操作符比较两个对象的引用,equals 方法比较两个对象的内容。在实际开发中,我们要根据具体的需求来选择使用哪个方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值