String, how many objects have you created ????

Source: Internet
Author: User
Java code
  1. String STR = new string ("AAA ");

How many string objects have been created in this line of code? The answer is 2, not 3. Because new string ("AAA") is equivalent to "AAA" and the original Instance Object created when it is placed in the heap, and the other is the "AAA" object in the constant pool, of course, STR itself is just a reference, which is placed in the stack to point to the object created in the heap.

The constant pool refers to the data that is identified during the compilation period and saved in the compiled. Class file. It includes constants in classes, methods, interfaces, and other fields, as well as string constants.

Java code
  1. String STR = "AAA ";

Create only one object. This involves the String constant pool. In JVM, there is a string pool that stores many string objects that can be shared. If we use the same literal string, it uses the same character string in the string pool. Of course, we can use the intern () method of the string object to access the constant object corresponding to the string object in the string pool.

When the above line of code is executed, JVM first goes to the string pool to check whether there are "AAA" objects. If so, no new objects will be created, directly return the reference of an existing object. If it does not exist, create the object first, add it to the string pool, and then return its reference.

 

Java code
  1. String str1 = "AAA ";
  2. String str2 = "AAA ";

Only one object is created. The above explanation makes it clearer. When the second line of code is executed, the AAA String object already exists in the pool, so the string object already exists in the pool is directly returned.

Java code
  1. String STR = "AAA" + "BBB ";

Or create only one object. Since the constant string is determined during compilation, and because both "AAA" and "BBB" are constants, the value of the variable STR can be determined during compilation. After this line of code is compiled, it is the same as string STR = "aaabbb"; which is different from what we usually do? Generally, when "+" is used to connect two strings, a new character object is generated. Let's take a look at the example below to understand:

Java code
  1. String str1 = "AAA ";
  2. String str2 = "BBB ";
  3. String str3 = "aaabbb ";
  4. String str4 = "AAA" + "BBB"; // No New String object will be generated
  5. System. Out. println (str3 = str4); // true
  6. Str4 = str1 + "BBB"; // A New String object is generated.
  7. System. Out. println (str3 = str4); // false
  8. Str4 = str1 + str2; // A New String object is generated.
  9. System. Out. println (str3 = str4); // false

From the above example, we can conclude that when the two strings connected by "+" are literal constant strings, if the strings connected by this connection exist in the pool, the object will not be re-created, instead, it directly references the string object in the pool. If one of the two strings connected by "+" is not a literal constant string (that is, defined), a new String object will be generated.
There are also exceptions in everything. This is no exception: If two or one of the strings connected by "+" is not a "literal constant", but if it is defined as a constant string, the situation changes again:

Java code
  1. Final string str1 = "AAA ";
  2. Final string str2 = "BBB ";
  3. String str3 = "aaabbb ";
  4. /*
  5. * Because both str1 and str2 are defined as constants, they can be determined during compilation and replaced by constants during compilation, which is equivalent
  6. * Str4 = "AAA" + "BBB", so no new object is generated
  7. */
  8. String str4 = str1 + str2;
  9. System. Out. println (str3 = str4); // true

However, if the final string is defined first but not initialized in the definition, it is initialized in the block as follows:

Java code
  1. // At this time, str1 and str2 are equivalent to variables, rather than variables, because the block can be determined only at runtime and cannot be determined at compilation.
  2. Final Static string str1;
  3. Final Static string str2;
  4. Static {
  5. Str1 = "AAA ";
  6. Str2 = "BBB ";
  7. }
  8. Public static void main (string [] ARGs ){
  9. String str3 = str1 + str2;
  10. String str4 = "aaabbb ";
  11. System. Out. println (str3 = str4); // The output is false.
  12. }
  13. String STR = ""; and string STR = new string ();
  14. STR = "" is put into the pool, but new string () is not put into the pool.

 

Intern () method of string
"When the intern method is called, if the pool already contains a string equal to this string object (this object is determined by the equals (object) method), the string in the pool is returned. Otherwise, add this string object to the pool and return a reference to this string object. It follows S and T for any two strings, when and only when S. when equals (t) is true, S. intern () = T. intern () is true ", which is the original JDK document annotation.

Now let's return to the example at the beginning. Why does string STR = new string ("AAA"); produce two objects? One is "AAA" and serves as a string constructor parameter, but "AAA" itself is a string. A character object has been created before being passed into the constructor, essentially, it is like the second instance: String STR = "AAA"; the string object it creates will be placed in the pool and referenced as the string object in the pool; the other is created through the new string () constructor. Therefore, new string ("AAA") generates two objects. That is to say, when a String object is created in this way, the string parameter object is first placed in the object pool, and then another string object is created.

After understanding why new string ("AAA") generates two objects, let's take a look at how new string (char value []) is created, will it put string objects in the pool? The answer is no. Because a character array is passed to the constructor, instead of putting the string parameter itself into the pool as if it were a literal constant string parameter. Now, if we prove that the new string (char value []) has not put the string object in the pool, we can write a simple test to open the XP Task Manager at runtime, check the memory usage of the operating system for preliminary confirmation:

Java code
  1. Int size = 10000000;
  2. Char C [] = new char [size];
  3. For (INT I = 0; I <size; I ++ ){
  4. C [I] = 'a ';
  5. }
  6. // When a string is created using a constructor with a character array parameter, the string object is not placed in the string pool.
  7. String str1 = new string (C );
  8. System. Out. println ("string String object created ...");
  9. Thread. Sleep (5000 );
  10. Str1.intern (); // you will see an increase in memory.
  11. System. Out. println ("Intern () is called for the first time ...");
  12. Thread. Sleep (5000 );
  13. Str1.intern (); // memory growth will not be seen in five seconds, because the pool has been there and will not be placed again, so the memory remains unchanged
  14. System. Out. println ("the second call intern () is completed ...");
  15. Thread. Sleep (5000 );

Therefore, there are two ways to create a String object and put it into the pool: the first is to directly use a literal constant to define a string, such as string STR = "AAA ";, STR references objects placed in the pool. The second is to use a string constructor with string parameters, and the input parameter value is in the String constant form, rather than the variable form, that is to say, it can only be string STR = new string ("AAA"); format, instead of defining string S = "AAA" first, and then using string STR = new string (s ); to create an object, new string (s); at this time, only one object is created, but if the pool does not exist, we can use the intern method to put it into the pool. Of course, the premise in the above pool is that these string objects do not exist in the pool.
In fact, when we study it carefully, we find that there is only one time in the pool, that is, directly using a literal constant string. The two timing mentioned above are actually directly using a literal string and putting it into the pool.

The stack in JVM is mentioned above. The following describes their respective functions:
Stack is used to save the reference of the basic type and object. Before the basic type is created, it will check whether there is an existing stack. If yes, it will point to it. If no, it will be created.
String stores strings in an array of strings. Therefore, it can be considered to be the same as char []. String A = "ABC". First, create an object in heap, go to the stack to check whether char [] exists. If yes, point to this address. If no, create an array in the stack.
New is in heap, and heap is used to store objects.

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.