首先是这段代码:
public class test {
public static void main(String[] args) {
String s1 = "Monday";
String s2 = new String("Monday");
if (s1 == s2){
System.out.println("s1 == s2");}
else{
System.out.println("s1 != s2");
}
if (s1.equals(s2)) {
System.out.println("s1 equals s2");
}else{
System.out.println("s1 not equals s2");
}
}
}
输出结果是:s1 != s2 和 s1 equals s2; 这个我可以理解,使用new操作符后,在堆内存中又新开辟了一块空间,s1和s2在堆内存中的值相同,但是引用的地址不同。但是在博客园的这篇文章看到下面这段代码:
public class test{
public static void main(String[] args)
{
test obj1 = new test();
test obj2 = new test();
if(obj1 == obj2){
System.out.println("obj1 == obj2");
}else{
System.out.println("obj1 != obj2");
}
if(obj1.equals(obj2)){
System.out.println("obj1 equals obj2");
}else{
System.out.println("obj1 not equals obj2");
}
}
}
输出: obj1 != obj2 obj1 not equals obj2
昨天晚上看了那篇文章的评论,把我自己的理解写出来,但是还是不是很清楚,我大意是这样的:
java当中所有类都继承自Object这个基类,在Object中的定义了一个equals方法,这个方法的初始化行为是比较对象的内存地址值(Object的equals方法使用==比较的),但在一些类库中这个方法被覆盖掉了,比如String, Interger, Date这些类中equals有其自己的实现方法,String类继承自Object类,也继承了equals方法,但是重写了该方法,不再比较类在堆内存中的存放地址了,而是比较存在堆中的值。 ???
这个解释不知道对不对,望指教,还有,关于obj1 not equals obj2你们是怎么看的??
==
Needless to say, comparison is a comparison of memory addresses in any case.equals
comparison is a method call. The default implementation (Object
class) uses==
:First code:
What is compared is the
String
object, and theString
class overrides theequals()
method and what is compared is the string content, sos1 equals s2
is output.You have no problem understanding this!
Second code:
What is compared is the
test
object. Thetest
class does not override theequals()
method, so the memory address is compared by default, thus outputtingobj1 not equals obj2
.I think we should put aside the code implementation first and look directly at the meaning of the equals method - to compare whether the object is the same.
So the question is, how can objects be considered the same? This is decided by the author of the class to which the object belongs, and only he can decide how instances of the class he creates should compare identically.
I don’t know who the author of String is, but do you agree with his equals implementation? When comparing strings, you should compare whether the character sequences are the same.
As for test, its author is you, and you did not define how to compare the test object, but you called equals to compare it. What will be the result at this time? It can only be the equals result of the parent class of the test class. The default parent class is the Object class. @ch_gilbert has already explained~
equals depends on the specific implementation
What you said is really complicated. After reading it, I should have a correct understanding
You can understand the comparison ofI'll post the source code of the Stirng.equals method, it's obvious.
==.
equals
To put it bluntly, isn’t it just a method? The exact result depends on how it is achieved, so why bother worrying about it?Suppose equals is written like this
How about, == is different, it only returns true if it is "111111".
Do you understand that?
equals
This thing is just a method! Because there is it inObject
, otherClass
s inherit it, so it looks special.