Software testing technology HOMEWORK1 the most impressive mistakes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.