The difference between = = and equal () in Java

Source: Internet
Author: User

= = and equal () are used to determine whether the two variables are equal.

(1) If the two variables are basic type variables, and are numeric (not necessarily the same data type), as long as the value is the same, will return true;

(2) If two variables are reference variables, = = only returns True if they point to the same object;

(3) The equal compares two strings, and returns True if the characters of the two strings correspond to equal properties.

int It=65;float f1=65.0f;//it==f1 is Truechar ch= ' A ';//it==ch is truestring str1 = new String ("Hello"); String str2 = new String ("Hello");//str1==str2 is false. They point to not the same object Str1.equal (STR2) is true

Note : The equal () method can be overridden.

The difference between = = and equal () in Java

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.