Java, == and equals the difference between understanding and memory allocation and variable storage location (heap, stack, constant pool area method, static method zone area)

the difference:

"==" comparison of the two reference points to in memory is not the same object (that is, the same memory space), which means that the storage location in the memory space are the same. (Reference types)

If the two reference objects are the same, "==" operator returns true (point to the same object), otherwise flase.

Note: If you have to memory allocation and variable storage location (heap, stack, constant pool area method, static method area district) interested can go and see this blog, which is written in great detail. I'm still learning to people is the foundation of great help to understand a lot of content, yet to be digested.

 

equals method provided by the Object class can be overridden by subclasses

Object class default implementation is as follows:

1 public boolean equals(Object obj) {
2 
3     return (this == obj);
4 
5 }

The default implementation returns true only when the object itself and will be compared, and this time "==" are equivalent.

Many Java class (the String class Date class File class) are all on the equals method has been rewritten to take a common example of the String class here.

Copy the code
 1 public class Test {
 2     public static void main(String[] args) {
 3         String str1 = "abc";
 4         String str2 = "abc";
 5         System.out.println(str1==str2);//true
 6 
 7 
 8         String str3 = new String("abc");
 9         String str4 = new String ("abc");
10         System.out.println(str3==str4);//false
11         System.out.println(str3.equals(str4));//true
12     }
13 }
Copy the code

Example line 5, we see the results of str1 == str2 is true

Analyze the reasons:

Look at the third line of code, create a reference to an object variable str1 String class in the stack, and then find by reference to a string constant pool there is no "abc", if not, then "abc" stored into the string constant pool. Here the constant pool is not "abc". That has created at compile time (defined directly by double quotation marks) "abc", it is stored in the constant pool. (If there are constant pool do not understand can look at my blog recommended above)

 

4th line of code and create a reference to an object of class String str2, and then find by reference to a string constant pool there is no "abc", if not, then "abc" stored into the string constant pool, and str2 point to make "abc", if you already have "abc" is the direct cause of str2 point to "abc". Here we have the third line of code in the "abc" This string is stored into the constant pool. So str1 and str2 point to the same "abc", returns true.

 

Line 8 and line 9 respectively of code creates two objects, and Str3 str4 points to different objects, i.e., the above mentioned different memory storage location. So str3 == str4 return is certainly false.

 

The first 11 lines of code str3.equals (str4) returns true

Because the String class overrides the equals method to compare whether the data is stored in the same memory space. Here are stored string "abc" it returns true.

 

to sum up:

Java language equals method is actually handed over to developers to override, so developers have to define what conditions meet two Object is equal of.

So we can not simply say that in the end what equals comparisons Yes. You want to know a class equals method is what is meant is to see the definition. (The above is a response from the user know almost)

We have to understand that the purpose of existence of equals.

Guess you like

Origin www.cnblogs.com/DSC1991/p/11963524.html