java = = and equals ()--java In a nutshell, 6th

Source: Internet
Author: User

= = for basic types, the comparison is whether their values are equal, for reference types, the comparison is in memory, and whether they point to the same object in memory.
All classes inherit the Equals () method of the object class, and the default implementation of this method uses the = = comparison, and some classes overload the Equals () method to compare the contents of the object.

We ' ve seen that primitive types and reference types differ significantly in the
They is assigned to variables, passed to methods, and copied. The types also differ
In the They is compared for equality. When used with primitive values, the
Equality operator (= =) Simply tests whether the values are identical (i.e., whether
They has exactly the same bits). With reference types, however, = = compares refer‐
ences, not actual objects. In other words, = = Tests whether, references refer to the
Same object; It does not test whether and objects have the same content.

When working with reference types, there is the kinds of equality:equality of ref‐
Erence and equality of object. It is important to distinguish between these, kinds
of equality. One of the "identical" when talking about
equality of references and the word "equal" when Talking about a distinct objects
that has the same content. To test-nonidentical objects for equality, pass one of the
them to the Equals () method of the other:

All objects inherit a Equals () method (from Object), but the default implementa‐
tion simply uses = = to test for identity of references, not equality of content. A class
That wants-to-allow objects-compared for equality can define its own version of
The Equals () method. Our point class does does this, but the String class does,
As indicated in the code example. You can call the Equals () method on a array, but
It is the same as using the = = operator, because arrays always inherit the default
Equals () method that compares references rather than array content. You can com‐
Pare arrays for equality with the convenience method Java.util.Arrays.equals ().

java = = and equals ()--java In a nutshell, 6th

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.