Java string comparison

Source: Internet
Author: User

Question: Determine if a string s is an empty string ""

Error method: s!= "" or s== ""

S!= "" or return True if string s is an empty string

The difference between = = and equals

Conclusion: Strings are object types and cannot be used to determine equality

Equals () compares the contents of the object (which distinguishes the case of letters), but is not equal if you compare two objects with "= =", compared to the memory addresses of two objects. Even though their contents are equal, the memory addresses of different objects are not the same.

The string is first an object.
As an example:
String STR0 = "abc";
String str1 = new String ("abc");

When executing string STR0 = "abc", the JVM creates three char-type values ' A ', ' B ' and ' C ' in the stack, and then creates a string object in the heap whose value is the array {' A ', ' B ', which consists of the three char-type values created in the stack just now , ' C '}, the value "ABC" of the newly created string object is added to the string pool. If we proceed to string Str1=new string ("abc"), the code, since "ABC" has been created and saved in the string pool, will only create a new string object in the heap, but its value is the "ABC" in the string pool to share the previous line of code execution;

Java string comparison

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.