. = = and Equal
. stack memory and pair memory
Make one thing clear and then another, so that the difference naturally comes out, and it's hard to say what you're saying.
The = = operator is specifically used to compare the values of two variables , that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator.
If the data that a variable points to is an object type, then this time involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as objet obj = new Object (); the variable obj is a memory, and new object () is another memory, At this point, the value stored in the memory of the variable obj corresponds to the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator.
The equals method is used to compare the contents of two separate objects , as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:
String A=new string ("foo");
String B=new string ("foo");
Two new statements create two objects, and then use a/b to point to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return false, and the contents of the two objects are the same. Therefore, the expression a.equals (b) returns True.
In real-world development, we often have to compare whether the string content passed in is, for example, string input = ...; Input.equals ("Quit"), many people do not pay attention to use = = to compare, this is wrong, casually from the Internet to find a few project real-life teaching video to see, there are a lot of such errors. Remember that the comparison of strings is basically using the Equals method.
If a class does not define its own Equals method, it inherits the Equals method of the object class, and the implementation code for the Equals method of the object class is as follows:
Boolean equals (Object o) {
return this==o;
}
This means that if a class does not define its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method, and write your own code to determine at what time that the contents of the two objects are the same.
Stack memory and heap memory
The reference variables for some of the basic types of variables and objects in the stack memory that are defined in the function are allocated in the function's stack memory.
Heap memory is used to hold objects and arrays created by new.
The difference between "= =" and the Equals method