A brief analysis of the "= =" and Equals () of string in Java

Source: Internet
Author: User

Just started to learn Java soon, always encounter string processing problems, not the wrong. Later decided to take a serious look at the string at the time of creation in the stack is how to do, just my own rookie level of understanding, there is any problem I hope you point out.

So first look at the following 8 statements:

1 New String ("Hello"); 2 New String ("Hello"); 3 String str3 = "Hello"; 4 String str4 = "Hello"; 5 String STR5 = "World"; 6 String str6 = "HelloWorld"; 7 String str7 = str4+str5; 8 String str8 = "Hello" + "world";

The string is a final class, and in each method that modifies the string value, a completely new string object is created. Then there are two classes when initializing a string, one for the object initialization (new) and the other as the base type, directly assigned.

When the string object is initialized, the heap memory query first exists for the existence of the object to be added, and if present, the newly declared reference points to an object that already exists in the heap, and if it does not, new memory is opened in the heap, the object is stored and the reference is directed to it, regardless of whether the object with the same value exists Creates a new chunk of memory in the stack memory to store the new reference.

When you initialize a string of the base type, the heap memory is also queried first, but here the query is divided into whether the object with the same value or the base type of the same value. If there are only objects of the same value, the new memory in the stack stores new references and points to objects of the same value in the heap, and if there is a base type of the same value in the heap, or objects and basic types, then the new reference is stored in the same stack memory as the original base type. That is, the stack memory address of the two base type reference is the same, referencing an object that points to the only identical value in the heap , and if there is no object or base type of the same value, the new memory storage base type is opened in the heap and new memory is stored in the stack for new references.

So it seems that the heap will not hold two identical strings. The initialization of the above 8 strings is allocated on the stack memory such as:

the "= =" comparison in the string type is whether the address referenced in the stack memory is equal. Equals () is the comparison of the values referenced in the stack memory for equality, that is, the object address to which the reference points is equal. Here you can query the address of S with System.identityhashcode (s) and query the value of S with S.hashcode ().

With the above analysis, and then look at the following questions, it seems that it is not very blindfolded:

1System.out.println (str1 = = str2);//false, obviously the two addresses in the stack memory are different2System.out.println (Str1.equals (str2));//true, both correspond to the same object in the heap3System.out.println (str2 = = STR3);//false, same basic type and object two methods of creation allocate different memory in the stack4System.out.println (Str2.equals (str2));//true, as long as the value is the same as. Equals () is the same as pointing to the same object in the heap5System.out.println (STR3 = = STR4);//true, the base type of the same value is allocated the same memory in the stack6System.out.println (STR4 = = STR5);//false, look at the picture ...7System.out.println (STR6 = = STR4+STR5);//when the false,string reference is added, the equivalent of a new object is created, then it must be the new stack memory8System.out.println (STR6 = = "Hello" + "world");//true,string The base type is added, or if a base type is created, it is equivalent to declaring a new string type "HelloWorld", which allocates the same stack memory as the existing base type reference

A brief analysis of the "= =" and Equals () of 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.