Typical Java string problems (new string (), string)

Source: Internet
Author: User
Original article: http://sarin.iteye.com/blog/603684

 

Let's look at an example. The Code is as follows:

Java code
  1. Public class test {
  2. Public static void main (string [] ARGs ){
  3. String STR = "ABC ";
  4. String str1 = "ABC ";
  5. String str2 = new string ("ABC ");
  6. System. Out. println (STR = str1 );
  7. System. Out. println (str1 = "ABC ");
  8. System. Out. println (str2 = "ABC ");
  9. System. Out. println (str1 = str2 );
  10. System. Out. println (str1.equals (str2 ));
  11. System. Out. println (str1 = str2.intern ());
  12. System. Out. println (str2 = str2.intern ());
  13. System. Out. println (str1.hashcode () = str2.hashcode ());
  14. }
  15. }

If you can directly judge the eight output results, the following analysis will be unnecessary. However, I think there are still many people who only have a superficial understanding of this string object. Next I will analyze the Java string class and object and its running mechanism.
A basic explanation of heap memory and stack memory problems. The data structure of the heap and stack is not explained here. When the Java language uses memory, the stack memory mainly stores the following content: the basic data type and object reference, while the heap memory storage object, the stack memory speed is faster than the heap memory. In summary, the object is referenced in the stack and the object is in the stack.
There are two comparison methods in Java: = and equals (). Equals () is the object class method. The equals () method defined in the object class is implemented as follows: java code

  1. Public Boolean equals (Object OBJ ){
  2. Return (this = OBJ );
  3. }

The string class overrides the equals () method and changes the equality principle of these types of objects. That is, the principle for determining whether an object is equal is to determine whether the content of the two is equal.
After learning about the above content, let's talk about string. The essence of the string class is the character array char []. Secondly, the string class is final and cannot be inherited. This may be ignored by most people, string is a special encapsulation type. You can assign values directly when using string or use new to create objects. However, the implementation mechanisms of the two are different. There is also the concept of a string pool. A string pool is maintained during Java runtime, and the string objects in the Pool cannot be repeated. If they are not created, leave it alone. The string pool does not belong to the heap and stack, but to the constant pool. The following analyzes the true meaning of the above Code. Java code

  1. String STR = "ABC ";
  2. String str1 = "ABC ";

The true meaning of the first sentence is to create an object "ABC" in the string pool, and then point STR to the object "ABC" in the pool when referencing ". When the second statement is executed, because "ABC" already exists in the string pool and is no longer created, STR = str1 returns true. Str1 = "ABC" must be correct. There is only one "ABC" in the string pool, and both STR and str1 point to "ABC" in the pool. Java code

  1. String str2 = new string ("ABC ");

This is a hot issue in Java SE. As we all know, two string objects are created in this sentence separately. Based on the above two sentences, only str2 references are created in the stack memory, create a String object in heap memory with the content "ABC", while str2 points to the first address of the heap memory object.
The following is the str2 = "ABC" problem. Obviously it is not. "ABC" is an object located in the string pool, and str2 points to the string object in the heap memory, = The address is determined. It is definitely not supported.
Str1.equals (str2): This is correct. As mentioned earlier, the equals of the string class overwrites the equals () method of the object class, which is actually used to determine whether the content is the same.
The following describes the intern () method. In the javadoc document, the intern () method is used to return the canonicalized representation of the string object. How to understand this sentence? The actual process is as follows: This method is used to check whether an object exists in the string pool. If an object exists, the reference of the object in the string pool is returned.
In this example, if the string pool contains "ABC", the "ABC" object reference in the pool will be returned when the intern () method is called, which is equivalent to str/str1, it is different from str2 because str2 points to heap memory.
The hashcode () method returns the hash code of the string content. Since the content is the same and the hash code must be the same, they are equal, which is easy to understand.
Let's look at the following example: Java code

  1. Public class test {
  2. Private Static string STR = "ABC ";
  3. Public static void main (string [] ARGs ){
  4. String str1 = "";
  5. String str2 = "BC ";
  6. String combo = str1 + str2;
  7. System. Out. println (STR = combo );
  8. System. Out. println (STR = combo. Intern ());
  9. }
  10. }

This example is used to show that the object is actually created in the heap content when the + String is connected. Combo points to the first address of the space in the heap memory storage "ABC" string, apparently STR = combo is incorrect, while STR = combo. intern () is correct. If "ABC" exists in the string pool, it is returned directly, and STR also points to the "ABC" object in the string pool. This example indicates that any re-Modification of string is to re-allocate the memory space, which makes the string objects do not interfere with each other. That is, once the content in the string is generated, it cannot be changed until a new object is generated.
At the same time, the problem is that the + connection string is used to generate a new object each time, and the object is in the heap memory, while the heap memory speed is relatively slow (relatively speaking ), therefore, when a large number of strings are connected, direct + is not desirable. Of course, an efficient method is required. The stringbuffer and stringbuilder provided by Java solve this problem. The difference is that the former is thread-safe while the latter is non-thread-safe. stringbuilder is available only after jdk1.5. Stringbuilder cannot guarantee higher efficiency than stringbuffer.
Since jdk1.5, when Java virtual machine executes string + operations, the internal implementation is also stringbuilder, which was previously implemented using stringbuffer.
Welcome to the discussion and hope it will be useful to users.

 

Related Article

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.