Easy-to-confuse string in Java

Source: Internet
Author: User

1 "= =" and "Equals"
The comparison of "= =" is a reference to a variable of two string types, that is, if two variables of type string, which refer to the same string object (that is, point to the same chunk of memory), the result of "= =" Comparison is true. The string object inherits from object and is overridden by the Equals () method, compared with the Equals () method of the object.
When a two string object is compared through the Equals () method, it is actually a comparison of the string content that is encapsulated by a string object, that is, if the string content of the two string object is the same (including the same case), the Equals () Method will return True.

2 string as the underlying type or as a string object

public class Str {
public static void Main (string[] args) {
String a = "abc";
String B = "abc";
String c = new String ("abc");
string S1 = new String ("abc");
String s2 = new String ("abc");
String s3 = S2;
System.out.println (A = = B); True
System.out.println (A.equals (b)); True
System.out.println (A = = c); False
System.out.println (A.equals (c)); True
System.out.println (S1 = = s2); False
System.out.println (s1.equals (S2)); True
SYSTEM.OUT.PRINTLN (s2 = = s3); True
System.out.println (S2.equals (S3)); True
}
}

String a = "abc", possibly creating or not creating an object, if the "ABC" string does not exist in the Java string pool, it creates a string object ("abc") in Java string dominant, and then str1 points to the memory address, Regardless of how many string objects in this way are created in the future, there is always only one memory address assigned, followed by a copy of string, called "string-resident" in Java, and all string constants will automatically reside after compilation. String is used as a base type, so the object referenced by S1 is within the string buffer pool.

String c = new String ("ABC"), which uses string as an object, so the object referenced by S2 is not in the string buffer pool, i.e. it is independent. With the new keyword, the virtual opportunity creates a new string object for this, allocating a new heap of memory to it. You can create at least one object, or two. Because the new keyword is used, a str2 string object will definitely be created in the heap, whose value is "abc". At the same time, if the string does not exist in the Java string pool, it will create the string object "ABC" in Java dominant.
String s3 = s2;//assigns the S2 reference to S3, so the object referenced by S3 is actually the object referenced by S2.

For more understanding of string types, visit http://blog.csdn.net/wangdong20/article/details/8566217

Easy-to-confuse string 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.