There are two types of comparisons: the comparison between variables, the comparison of variables and values,
The reference type value is divided into two types: "ABCD", New String ("ABCD"); "ABCD" is equivalent to a in a= "ABCD", and the new string ("ABCD") is equivalent to B in b= new string ("ABCD");
New to allocate a newer memory address
1. The basic type variable is used = =; Because the basic data type has no equal method
2. Reference type variable: (Reference type: Class, interface, array)
= =: Compare their in-memory addresses, i.e. reference equal
equals:string integers are compared with equal values other than the Equals method comparison is reference equality
Note:
int a = 3;
int b = 3;
int c = 4;
Because the basic data type size is known, the life cycle is known (defined in the method, the method exits after the automatic destruction), so the pursuit of speed, stored in the stack (only refers to local variables), when the definition of the field 3 to a, then define B, first find there is no 3 for the field address, if so, let B also point to That is, the address of A and B is the same, the last two lines of code is equivalent to int b = A; When defining C, found that there is no field 4, then re-open the address of the field 4, let reference c point to field 4. String (If a string is strings s = "abc"; it is placed in the stack, if a string is used to create the object in the way string s = new String ("abc"); it's in the heap, and if it's a simple "abc", the input string constant is placed on the static segement/stack/string dominant), when executing = There is also a procedure similar to first looking for the same string. ToString () method?
Java Equality comparison: = = and equals