On the usage of = = and equals methods in Java
The equals method is Java Method of lang.Object class.
There are two usage instructions:
(1) For string variables, the "= =" and "equals()" methods are different when comparing strings.
"= =" compare the values of the two variables themselves, that is, the first address of the two objects in memory.
"Equals()" compares whether the contents contained in the string are the same.
For example:
Then:
Note (1):
If: StringBuffer S1 = new StringBuffer ("a"); StringBuffer s2 = new StringBuffer("a");
Results: S1 Equals (S2) / / false
Explanation:
The equals method is not redefined in the StringBuffer class, so it comes from the object class. The equals method in the object class is used to compare "addresses", so it is equal to false
Note (2):
For S3 and S4, there is one difference to be noticed, because S3 and S4 are two characters
A variable generated by a string constant in which the memory addresses are equal,
So S3 = = S4 is true (even if there is no assignment statement such as S3 = S4)
(2) For non string variables, "= =" and "equals" methods are used to compare the first address of their object in heap memory, that is, to compare whether two reference variables point to the same object.
For example:
Then: obj1 = = obj2 is false Equals (obj2) is false
However, if such a sentence is added: obj1 = obj2;
Then obj1 = = obj2 is true Equals (obj2) is true
In short: the equals method compares the contents of strings, and compares whether the objects it points to are the same for non strings.
==The comparator also compares whether the objects pointed to are the same, that is, the first address of the object in the pair memory.
The equals method is redefined in the string class, and the value is compared, not the address. So it's true.
The difference between equals and = = can be seen from the following aspects:
(1) For basic type comparison, you can only use = = instead of equals
For example:
(2) For package types of basic types, such as boolean, character, byte, shot, integer, long, float, double and other reference variables, = = refers to the comparison address, while equals refers to the comparison content. For example:
This is an instance of integer. The same is true for other instances, such as double, character, float, etc.
(3) Note: the three classes of string, StringBuffer and StringBuilder are further described.
(a) First, let's introduce the usage of string. Please see the following example:
Answer explanation: S1 and S2 respectively point to the object created by the string constant "123". In the constant pool, there is only one object with the content of 123. There are two references S1 and S2 pointing to this object, so the addresses pointed to by the two reference variables are the same, Therefore, the running result at (1) is true, because s1.equals (S2) compares whether the contents of the objects pointed to by S1 and S2 are equal, and we know that the contents of these two objects are string constants "123", Therefore, the running result at mark (2) is true. Using the same method, the objects pointed to by S1 and S3 are different, and the content is also different. Therefore, the running result at marks (3) and (4) is false.
Look at S4 and S5, The contents of the objects pointed to by the two reference variables are the same (the contents are both 123), but the two objects are created by the new operator, and two pieces of space are allocated to the two objects in memory. Therefore, the memory addresses of the two objects are different. There are two different objects in the story (5) The running result of S4 = = S5 at is false, but the content is the same, so the running result of s4.equals (S5) at tag (6) is true. Similarly, S4 and S6 point to different object addresses and different contents. Therefore, the operation result at mark (7) (8) is false.
S1 and S4 point to two different objects respectively (the reason why they are called this is because the addresses of the two objects in memory are different, so the objects are different). Therefore, the running result of S1 = = S4 marked at (9) is false, and the running result of s1.equals (S4) marked at (10) is true
(4) Let's take another look at an example (this example is the example in Chapter 3 of Java programming thought):
Question about the running result: at first glance, I'm a little surprised. Why isn't it true? Doesn't it mean that the equals method compares the content?
Explanation: Yes, if the equals method is overridden in the new class, it can be used to compare the contents. However, in the above example, class value does not override the equals method in the object, but inherits the method. Therefore, it is used to compare addresses, and the objects pointed to by V1 and V2 are different. Therefore, the running result of v1.equals (V2) at mark (1) is false, and the running result of V1 = = V2 at mark (2) is also false.
That's all for the introduction of equals and = =. If there is a better or updated explanation, please give us more advice. Thank you.
summary
The above is all about the usage of = = and equals methods in Java. I hope it will be helpful to you. Interested friends can continue to refer to this site: examples of URL + urlconnection usage method of Java network programming, introduction to creating and running a java thread method, code examples of java encryption and decryption rsa usage method, etc. thank you for your support to this site!