java相等_Java 判断相等

1.除 float 和 double 外的原始数据类型 : 使用 ==

long a = (long) 1234567890;long b = (long) 1234567890;if (a ==b) {

System.out.println("基本数据类型相等");

}

2.包装类使用 equals 或者转换为基本数据类型再用 ==

Long a = (long) 1234567890;

Long b= (long) 1234567890;if (a != null &&a.equals(b)) {

System.out.println("包装类相等");

}if (a != null && a.longValue() ==b.longValue()) {

System.out.println("包装类相等");

}

3.对象要用 equals

String a1= null;

String b1= newString();if (a1 != null && a1.length() > 0 &&a1.equals(b1)) {

System.out.println("对象相等");

}

附录:

public static void main(String[] args) {

System.out.println("-----");

int a = 200;

int a2 = 200;

if (a == a2) {

System.out.println("相等");//√

}

System.out.println("-----");

Integer b = 200;

Integer b2 = 200;

if (b == b2) {

System.out.println("相等");//-128~127√

}

if (b.equals(b2)) {

System.out.println("相等2");//√

}

System.out.println("-----");

Integer c = new Integer(200);

Integer c2 = new Integer(200);

if (c == c2) {

System.out.println("相等");

}

if (c.equals(c2)) {

System.out.println("相等2");//√

}

System.out.println("-----");

if (a==c){

System.out.println("相等");//√

}

if (c.equals(a)){

System.out.println("相等2");//√

}

if (b==c){

System.out.println("相等3");

}

if (c.equals(b)){

System.out.println("相等4");//√

}

}

public static void main(String[] args) {

System.out.println("-----");

String a = "a";

String a2 = "a";

if (a == a2) {

System.out.println("相等");//√

}

if (a.equals(a2)) {

System.out.println("相等2");//√

}

System.out.println("-----");

String b = new String("a");

String b2 = new String("a");

if (b == b2) {

System.out.println("相等");

}

if (b.equals(b2)) {

System.out.println("相等2");//√

}

System.out.println("-----");

if (a == b) {

System.out.println("相等");

}

if (a.equals(b)) {

System.out.println("相等2");//√

}

}

public static void main(String[] args) {

System.out.println("-----");

boolean a = true;

boolean a2 = true;

if (a == a2) {

System.out.println("相等");//√

}

System.out.println("-----");

Boolean b = true;

Boolean b2 = true;

if (b == b2) {

System.out.println("相等");//√

}

if (b.equals(b2)) {

System.out.println("相等2");//√

}

System.out.println("-----");

Boolean c = new Boolean(true);

Boolean c2 = new Boolean(true);

if (c == c2) {

System.out.println("相等");

}

if (c.equals(c2)) {

System.out.println("相等2");//√

}

System.out.println("-----");

if (a == c) {

System.out.println("相等");//√

}

if (c.equals(a)) {

System.out.println("相等2");//√

}

if (b == c) {

System.out.println("相等3");

}

if (c.equals(b)) {

System.out.println("相等4");//√

}

}

超类 Object 中有这个 equals() 方法,该方法主要用于比较两个对象是否相等。该方法的源码如下:

public boolean equals(Object obj) {

return (this == obj);

}

我们知道所有的对象都拥有标识(内存地址)和状态(数据),同时“==”比较两个对象的的内存地址,所以说使用 Object 的 equals() 方法是比较两个对象的内存地址是否相等,即若 object1.equals(object2) 为 true,则表示 equals1 和 equals2 实际上是引用同一个对象。虽然有时候 Object 的 equals() 方法可以满足我们一些基本的要求,但是我们必须要清楚我们很大部分时间都是进行两个对象的比较,这个时候 Object 的 equals() 方法就不可以了,实际上 JDK 中,String、Math 等封装类都对 equals() 方法进行了重写。

在 Java 规范中,它对 equals() 方法的使用必须要遵循如下几个规则:

equals 方法在非空对象引用上实现相等关系:

1、自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。

2、对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。

3、传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。

4、一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。

5、对于任何非空引用值 x,x.equals(null) 都应返回 false。

对于上面几个规则,我们在使用的过程中最好遵守,否则会出现意想不到的错误。

在 java 中进行比较,我们需要根据比较的类型来选择合适的比较方式:

1) 对象域,使用 equals 方法 。

2) 类型安全的枚举,使用 equals 或== 。

3) 可能为 null 的对象域 : 使用 == 和 equals 。

4) 数组域 : 使用 Arrays.equals 。

5)除 float 和 double 外的原始数据类型 : 使用 == 。

6) float 类型: 使用 Float.foatToIntBits 转换成 int 类型,然后使用==。

7) double 类型: 使用 Double.doubleToLongBit 转换成 long 类型,然后使用==。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值