"Meow" on the Android Road "" foreign article "about = = and equals in the actual programming, often use = = and equals to determine whether the variable is the same. But these two ways of comparison are often foggy to the mind. Here is my personal summary, hoping to play a role in dispel. "Popular Before speaking" please read "Meow" on the Android Road "Basic (i)" "Java Object-oriented" data types and operators understand Java basic data types and reference data types 1, the "= =" operator popular point, the = = operator compares the contents of two variables in the stack is the same. Take a = = B For example: 1.1 If a, a, is a basic data type
- A, B is incompatible and cannot be compared
- A, B compatible, compare the values in the stack
// Code: int a = +; int B =+//Result:true
1.2 If a is a basic data type, B is a reference type
- A, B is incompatible and cannot be compared
- A, B is compatible, then the comparison is still a, the contents of the stack
1 // Code: 2 int a = +; 3 Integer b = +; 4 New Integer (+); 5 6 // Result: 7 A = = B:true8 A = = C:true
1.3 If both A and B are reference types
- A, b incompatible, can not compare
- A, B compatible, the comparison is the address of object A, B in the stack (regardless of NULL)
1 //to put it simply, the comparison between A and B points to the same instance (block of memory). 2 Code:3String a = "Test";4String B = "Test";5String C =NewString ("Test");6String d =NewString ("Test");7 8Integer x = 1000;9Integer y =NewInteger (1000);TenInteger z =NewInteger (1000); One A Result: -A = = B:true -A = = C:false thec = = d:false - -x = = y:false -y = = Z:false + //in the example above, "Test" is placed in a constant pool, and the address of object A and B points to the constant. C and D re-open memory inside the heap to store "Test", so C and D point to the memory address is not the same.
2, the Equals method to remove the custom Equals method, and some classes on the Equals method of the special implementation, in general, equals compares the two objects are the actual content is the same. Also take B.equals (a) For example: 2.1 If both A and B are basic data types, you cannot compare 2.2 if a is a basic data type and B is a reference type
- If A and B are incompatible, the result must be false
- If A and B are compatible, compare the values of a in the stack with the value of B in heap memory
1 // Code: 2 int a = +; 3 Integer b = +; 4 New Integer (+); 5 6 // Result: 7 B.equals (a):true8 c.equals (a):true
2.3 If both A and B are reference types
- If A and B are not the same instance, the result must be false
1 // Code: 2 New String ("Test"); 3 New StringBuilder ("Test"); 4 5 // Result: 6 B.equals (a):false
- If A and B are the same instance, compare the values of A and B in the heap memory for the same
//Code:String a = "Test"; String b= "Test"; String C=NewString ("Test"); String D=NewString ("Test"); Integer x= 1000; Integer y=NewInteger (1000); Integer z=NewInteger (1000); //Result:B.equals (a):trueC.equals (a):trueC.equals (d):truey.equals (x):trueY.equals (z):true
This article is Nodin original, reproduced please indicate the source! Http://www.cnblogs.com/monodin/p/3841219.html