"=" And equals Methods

Source: Internet
Author: User

1,

= The operator is used to compare whether the values of variables are equal. A better understanding is:
Int A = 10;
Int B = 10;
Then a = B will be true.
But what is hard to understand is:
String A = new string ("foo ");
String B = new string ("foo ");
If a = B, false is returned.

Pair
Variables are actually a reference. Their values point to the memory address of the object, rather than the object itself.


. Both A and B use the new operator, which means that two "foo" characters will be generated in the memory.
String. Since they are two, they are naturally located at different memory addresses. The values of A and B are actually two different memory address values. Therefore, the result is false when the "=" operator is used. True: A and
B Refers to objects whose content is "foo" and should be "equal", but the = operator does not involve the comparison of object content.
The comparison of object content is exactly what the equals method does.

2,

Public static void main (string [] ARGs)
{
String Ss = new string ("GDD ");
String dd = new string ("GDD ");
System. Out. println (ss. Equals (dd ));
System. Out. println (Ss = dd );
}

Result:

True
False

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.