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 = new String();
if (a1 != null && a1.length() > 0 && a1.equals(b1)) { System.out.println("对象相等"); }

 

 

附录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 类型,然后使用==。

posted @   草木物语  阅读(613)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示