Notes for comparing strings in Java

Source: Internet
Author: User
People familiar with C ++ compared two strings CodeBe sure to understand:

(String1 = string2)

However, in Java, this code returns false even if the two strings are identical.

In Java, string1.equals (string2) must be used for judgment.

Supplement

If:

String S1 = new string ("hello ");

String S2 = new string ("hello ");

Then (S1 = S2) = false

If:

String S1 = "hello ";

String S2 = "hello ";

Then (S1 = S2) = true;

Because they point to the same object.

If the values of other variables are assigned to S1 and S2, false is returned even if the content is the same because it does not point to the same object. Therefore, we recommend that you use equals () Because equals compares the actual content.

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.