Difference between = and equals () in Java

Source: Internet
Author: User

= Only one-sided comparisons can be made. Why? When comparing basic types, it compares actual values. When comparing object types, because the objects in Java are referenced by addresses, so = only one-sided comparison of the address, rather than in-depth investigation.

The equals () method has the same effect as =, but equals () is a method after all. People can override it to change its original functions. For example, after overwriting, The equals () method may become a comparison of object values.

Note that some objects in Java have already overwritten the equals () method, such as string. In this way, when comparing strings, The equals () method compares values.

2. If (STR = NULL | Str. Equals ("")){}

Pay attention to two issues:

(1) determine the null value of the row to be selected

(2) Use equals () instead of = for comparison.

(3) When comparing null strings, note the space in the middle.


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.