Week six assignment 2013551605

Source: Internet
Author: User

1. What is the difference between "= =" and Equals method?

For:The = = operator is specifically used to compare the values of two variables for equality, that is, to compare the memory stored in the variables corresponding to the
to compare two basic types of data or two reference variables for equality, you can only use = = to manipulate
characters.
if the data that a variable points to is an object type, then it involves two blocks of memory, and the object itself occupies a
memory (heap memory), variables also occupy a piece of memory, such as Objet obj = NewObject (); variable obj is a memory,
The new Object () is another memory, at which point the value stored in the memory of the variable obj is the one that the object occupies
the first address of the block memory. For a variable that points to an object type, if you want to compare whether two variables point to the same object, the
to see if the values in memory for these two variables are equal, you need to compare them with the = = operator.
The equals method is used to compare whether the contents of two separate objects are the same, just like to compare the appearance of a two person
, it compares two objects that are independent of each other. For example, for the following code:
string A=new string ("foo");
string B=new string ("foo");
two new statements create two objects, and then use the A/b variables to point to one of the objects, two
different objects, their first addresses are different, that is, the values stored in a and B are not the same, so the expression
The a==b will return false, and the contents of the two objects are the same, so the expression a.equals (b) will return
true.
in real-world development, we often want to compare whether the string content passed in is not equal, for example, string input
= ... ..; Input.equals ("quit"), many people do not pay attention to use = = to compare, this is wrong, casually from the Internet
find a few project actual teaching video to see, there is a large number of such errors. Remember, the comparison of strings is basically
is to use the Equals method.
If a class does not define its own Equals method, it inherits the Equals method of the object class, and the object class
The implementation code for the Equals method is as follows:
Boolean equals (Object o) {
return this==o;
}

Week six assignment 2013551605

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.