One of the most memorable mistakes I've ever had was about = = and equals, which was encountered while writing the database.
Like what:
String a = "123";
String b = new String ("123");
System.out.println (b. Equals (a));
System.out.println (b = = a);
The result is true for the former and false for the latter.
Later, after consulting the information, I found:
= = Compares the value of the reference variable, which is the memory address of the object being compared. Returns True when two reference variables point to the same object, or false instead.
In object, the Equals method is declared as follows:
public boolean equals (Object obj) {
return (this = = obj);
}
As you can see, in object, Equals is implemented by = =, so the equals comparison under object is also the value of the reference variable.
But in string, the Equals method is declared as follows:
public boolean equals (Object anobject) {
if (this = = AnObject) {
return true;
}
if (anobject instanceof String) {
String anotherstring = (string) anobject;
int n = value.length;
if (n = = anotherString.value.length) {
Char v1[] = value;
Char v2[] = Anotherstring.value;
int i = 0;
while (n--! = 0) {
if (v1[i]! = V2[i])
return false;
i++;
}
return true;
}
}
return false;
}
As you can see, in sring, it overrides the Equals method. The first thing to judge is whether the same object is the same, and returns True if the value of two objects is the same.
Software testing technology HOMEWORK1 the most impressive mistakes