In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to compare the similarities and differences between equals () method and = =, many beginners are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
The equals () method is a method of the Object class, and all classes integrate this method, and some classes override this method. Let's take a look at the source code for this method in the Object class:
Public boolean equals (Object obj) {
Return (this = = obj)
}
As you can see, the equals () method inherited from Object uses = = at the bottom, that is, the address values are compared between the two.
Let's talk about String again, which overrides the equals () method with the following source code:
Public boolean equals (Object anObject) {
If (this = = anObject) {
Return true
}
If (anObject instanceof String) {
String anotherString = (String) anObject
Int n = value.length
If (n = = anotherString.value.length) {
Char v1 [] = value
Char v2 [] = anotherString.value
Int I = 0
While (nMurt -! = 0) {
If (v1 [I]! = v2 [I])
Return false
ITunes +
}
Return true
}
}
Return false
}
In this method, first use = = to compare. If the address values of the two are the same, return true. If the address values are different, then determine whether the string content is the same. If so, the return value is also true. We can do an experiment:
Class equalsTest {
Public static void main (String [] args) {
String a = "abc"
String b = "abc"
String c = new String ("abc")
Sop (astatb); / / true
Sop (a.equals (b)); / / true
Sop (astatc); / / false
Sop (a.equals (c)); / / true
}
Public static void sop (Object o) {
System.out.println (o)
}
}
In this experiment, the shared meta design pattern started by default in variables an and b both point to the string object "abc", so the address value is the same, while the bottom layer of the statement new String ("abc") creates two objects, so the address value of c is different from that of aformab, and the result judged by = = is false, but the string content of c is the same as that of a, so the result judged by equals () is ture.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.