“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
== and equals
Always forget. Write it down. == Has different effects on basic types and reference types. 1: Values are compared for basic types. 2: reference addresses are compared for reference types
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b); //true
System.out.println(a == c); //false
Copy the code
For the comparison between a and B, because a and B both refer to “hello” in the string constant pool, all == results in true, while C refers to “Hello” in the heap, so false.
Equals equals is essentially equal to ==, except that String and Integer have overridden the equals method to make it a value comparison. The eqauls method in the object class:
Public Boolean equals(Object obj) {return (this == obj); // Same as ==}Copy the code
String c = new String("hello"); String d = new String("hello"); System.out.println(c== d); //false System.out.println(c.equals(d)); //trueCopy the code
== equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals
Eqauls method in String:
if (this == anObject) { return true; } if (anObject instanceof String) {String anotherString = (String)anObject; Int n = value. Length; If (n = = anotherString. Value. Length) {/ / one by one, to compare each character char v1 [] = value; Char v2 [] = anotherString. Value; int i = 0; while (n-- ! = 0) {if (v1[I]! [I] = v2 return false. i++; } return true; } } return false; }Copy the code
The difference between value passing and reference passing
Sample code:
public static void main(String[] args) {
int b = 0;
change(b);
System.out.println(b);
}
public static void change(int a)
{
a = 1;
}
Copy the code
Results:
Why can’t the value of b be changed to 1? We know that each method execution generates a stack frame in the virtual machine stack, and this stack frame stores local variables, operand stacks, dynamic links, and so on.
The int A in the change method is stored in the local variable table. But for int B, it’s stored in the heap. So a and B are two different things.
And int A in the stack frame is just a copy of int B in the heap.
Sample code:
public static void main(String[] args) {
StringBuffer str = new StringBuffer("hello");
ReferencePassing(str);
System.out.println(str);
}
public static void ReferencePassing(StringBuffer sb)
{
sb.append(" world");
}
Copy the code
Results:
You can obviously see that the value of STR has changed.
When ReferencePassing is implemented, the vm stack also generates a stack frame. The local variable table contains a reference to sb, which points to the STR object in the heap.
That’s why STR objects in the heap are modified.
Conclusion:
== is a value comparison for primitive types, and a reference comparison for reference types; Equals by default is a reference comparison, but many classes override the equals method (String, Integer, etc.) to make it a value comparison, so in general equals compares whether values are equal.