Determining whether strings are equal in Java

Source: Internet
Author: User

Weekend teacher Let write a guess the number of Java games, randomly generate a different four-digit number, and then according to the user input to judge, give the corresponding results

Each time you enter a 4-digit number, according to the standard,
is divided into digital existence (recorded as B);
The number exists and is in the correct position (recorded as a)

Example: Generate a number 1234
Guessing number: 1367 (hint Quiz conclusion: 1A1B)

And then I have the result. The return type of the definition is a string, which is then received with a string Jieguo, which should be compared to the string "4 a" when it is finally judged whether to guess the number.

At first I used the IF (jieguo== "4 A") to determine whether two strings were the same, but found no.

So find the mother found the difference:

When comparing strings, if you comparetwo objects with "= =", the memory addresses of two objects are compared, so they are not equal. Even though their contents are equal, the memory addresses of different objects are not the same.

So there is the Equals ();

For example:
String A=new string ("AAA");
String B=new string ("AAA");
The two strings should of course be equal.
If a==b with an expression, the value of the expression is false
If A.equals (B) is used with an expression, the value of the expression is true;

Equals () compares the contents of the object (case-sensitive letters).

Determining whether strings are 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.