Java中对象的比较 == 和 equals() -Java 学习笔记 (15)
来源:互联网 发布:如何取消8080端口占用 编辑:程序博客网 时间:2024/05/01 04:24
Java在对象对比时可以用符号和"=="方法 equals();
"=="符号只是单纯的比较对象引用的指针是否相等。我们知道在Java中,Object myObject 其中myObject是引用变量,是指向具体堆内存中对象的指针。也就是说Object a,b 若a == b 则说明a和b指向同一个对象,若a和b指向不同的对象,即使这两个对象完全相同,a != b
所有类都会从Object类中继承equals()方法,一般情况下equals()方法判断对象(在对内存中的实体)是否相等,如果相等则返回true,不相等则返回false。当然,具体情况还要看类的创建者是怎么重写(Override) equals()方法的。
先看一个Java系统类对象的例子
结果是
false
true
分析:
i1和i2虽然值相等,但它们是两个不同的引用,所以 i1 != i2
我们来看一下Java SE 6 Docs关于Integer的equals()方法的描述
运行结果
false
true
false
false
分析:
m1和m2对象值完全相等,但并没有像我们想象中的结果m1.equals(m2)==true
实际上m1.equals(m2)==false 这是因为我们没有重写equals()方法,Object中默认的equals()方法和"=="一样只是判断引用指针是否相等,参看一下Object类的API Docs
"=="符号只是单纯的比较对象引用的指针是否相等。我们知道在Java中,Object myObject 其中myObject是引用变量,是指向具体堆内存中对象的指针。也就是说Object a,b 若a == b 则说明a和b指向同一个对象,若a和b指向不同的对象,即使这两个对象完全相同,a != b
所有类都会从Object类中继承equals()方法,一般情况下equals()方法判断对象(在对内存中的实体)是否相等,如果相等则返回true,不相等则返回false。当然,具体情况还要看类的创建者是怎么重写(Override) equals()方法的。
先看一个Java系统类对象的例子
public class EqualsTest ...{
public static void main(String[] args) ...{
Integer i1=new Integer(1);
Integer i2=new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
}
}
public static void main(String[] args) ...{
Integer i1=new Integer(1);
Integer i2=new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
}
}
结果是
false
true
分析:
i1和i2虽然值相等,但它们是两个不同的引用,所以 i1 != i2
我们来看一下Java SE 6 Docs关于Integer的equals()方法的描述
equals
public boolean equals(Object obj)
- Compares this object to the specified object. The result is
true
if and only if the argument is notnull
and is anInteger
object that contains the sameint
value as this object. - Overrides:
equals
in classObject
- Parameters:
obj
- the object to compare with.- Returns:
true
if the objects are the same;false
otherwise.
只有参数obj不为空并是Integer型,包含相同的整型值才返回true。
i1和i2符合上述条件,当然返回true了
加上自定义类,写段代码再试试
public class EqualsTest ...{
public static void main(String[] args) ...{
Integer i1=new Integer(1);
Integer i2=new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
MyObject m1=new MyObject(1);
MyObject m2=new MyObject(1);
System.out.println(m1 == m2);
System.out.println(m1.equals(m2));
}
}
class MyObject ...{
private int i;
public MyObject(int i) ...{
this.i = i;
}
}
public static void main(String[] args) ...{
Integer i1=new Integer(1);
Integer i2=new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
MyObject m1=new MyObject(1);
MyObject m2=new MyObject(1);
System.out.println(m1 == m2);
System.out.println(m1.equals(m2));
}
}
class MyObject ...{
private int i;
public MyObject(int i) ...{
this.i = i;
}
}
false
true
false
false
分析:
m1和m2对象值完全相等,但并没有像我们想象中的结果m1.equals(m2)==true
实际上m1.equals(m2)==false 这是因为我们没有重写equals()方法,Object中默认的equals()方法和"=="一样只是判断引用指针是否相等,参看一下Object类的API Docs
equals
public boolean equals(Object obj)
- Indicates whether some other object is "equal to" this one.
The
equals
method implements an equivalence relation on non-null object references:- It is reflexive: for any non-null reference value
x
,x.equals(x)
should returntrue
. - It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations of x.equals(y) consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
- Parameters:
obj
- the reference object with which to compare.- Returns:
true
if this object is the same as the obj argument;false
otherwise.
- It is reflexive: for any non-null reference value
- 所以,以上代码需要改造,加入equals()方法的重载public class EqualsTest ...{
public static void main(String[] args) ...{
Integer i1=new Integer(1);
Integer i2=new Integer(1);
System.out.println(i1 == i2);
System.out.println(i1.equals(i2));
MyObject m1=new MyObject(1);
MyObject m2=new MyObject(1);
System.out.println(m1 == m2);
System.out.println(m1.equals(m2));
}
}
class MyObject ...{
private int i;
public MyObject(int i) ...{
this.i = i;
}
public boolean equals(Object obj) ...{
if(obj==null) ...{
return false;
}
else ...{
if(obj instanceof MyObject) ...{
MyObject m = (MyObject)obj;
if(m.i==this.i) ...{
return true;
}
}
}
return false;
}
}
运行结果:
false
true
false
true
一切OK ~
- Java中对象的比较 == 和 equals() -Java 学习笔记 (15)
- Java中对象的比较 == 和 equals()
- Java中对象的比较 == 和 equals()
- java中==与 String中 equals 和对象中equals比较
- Java对象之间的比较之equals和==
- Java中两个对象的比较 equals()方法和==号的区别
- JAVA中equals()和==比较
- Java中对象的equals比较
- 【JAVA学习】java中==、equals()、hashCode()都和对象的比较有关,在java中这三者各有什么用处呢,即java中为什么需要设计这三种对象的比较方法呢?
- java中equals方法和“==”的比较
- java 中 == 和 equals()方法的 比较
- java中equals方法和“==”的比较
- Java中“==”和equals的比较
- java中equals方法和“==”的比较
- java中equals方法和“==”的比较
- java中==号和equals的比较
- 在java中equals和“==”的比较
- java中"equals"和"=="的区别比较
- 做人不要"太在乎"
- Oracle WITH clause
- 打破记录-1.5天搞定毕业论文
- python模块之quopri: quoted-printable编解码
- 用Javascript评估用户输入密码的强度
- Java中对象的比较 == 和 equals() -Java 学习笔记 (15)
- 金庸感人的经典爱情对白
- 伤感情话50句
- .net的DateTime加上一段时间
- java简史
- ASP.NET页面传值方式
- 最近想了解了解PHP,写点入门感受。
- 让你哭笑不得的QQ留言
- DIV+CSS布局的网页对网站SEO影响