In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the difference between Java's" = "and equals method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Let's start with "=":
The variable "= =" of the basic data type (byte,short,char,int,long,float,double,boolean) compares whether the values of the two variables are equal.
For example: int a = 3; int b = 3; astatb; return is true
For reference types such as, the address that the variable points to is compared.
Take our most commonly used string type as an example:
For example: String a = "abc"; String b = "abc"
In this case, the string is assigned directly to the variable, and the string goes into the constant pool. When "abc" is assigned to a for the first time, it goes to the constant pool to see if there is a string "abc". If so, point a to the address of the string in the constant pool. If not, create it in the constant pool. When the second assignment assigns "abc" to b, it also looks for the string "abc" in the constant pool, and then assigns his address to b.
So what we return is true when we do the astatb operation.
Let's look at another situation: String a = new String ("abc"); String b = new String ("abc")
The "abc" object created in this case is stored in the heap, and the "abc" pointed to by an and b are stored in different locations in the heap memory. What is stored in an and b is the memory header address of "abc" in the heap. These two addresses are obviously different, so what we return is false when we do the astatb operation.
Let's take a look at "equals":
Equals methods are defined in the Object class, and all classes inherit from the Object class, so all classes have equals methods.
Let's look at the source code of the equals method:
Public boolean equals (Object obj) {
Return (this = = obj)
}
You can see that "= =" is also used for comparison in the equals method of the Object class, so it is equivalent to "= =" when making a comparison, but why is the result different when we compare strings?
The reason is that the String type overrides the equals method. Let's look at the 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
}
From the source code, we can see that the characters of the string are compared one by one in the equals method of String. If they are all the same, return true. So for the equals method in String, the comparison is between the contents of two strings for:
String a = new String ("abc"); String b = new String ("abc")
Because the contents of an and b are the same, return true.
Summary:
For string comparison "=" compares the addresses of two strings
For string comparison "equals" compares the contents of two strings
This is the end of the content of "what is the difference between Java's" = = "and equals method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.