Classic question for "Go" Java String Object (new String ())

Source: Internet
Author: User
Tags java se

public class Test {public      static void Main (string[] args) {          String str = "abc";          String str1 = "abc";          String str2 = new String ("abc");          System.out.println (str = = str1);          System.out.println (str1 = = "abc");          System.out.println (str2 = = "abc");          System.out.println (str1 = = str2);          System.out.println (Str1.equals (str2));          System.out.println (str1 = = Str2.intern ());          System.out.println (str2 = = Str2.intern ());          System.out.println (str1.hashcode () = = Str2.hashcode ());}  }  

If you can directly determine the results of these 8 outputs, the following analysis will not be seen. But I think a lot of people have a superficial understanding of the problem with this string object, and the following is an analysis of the Java language string classes and objects and their operational mechanisms.
Make a basic description of the heap memory and stack memory issues. The data structures of heaps and stacks are not explained here. When using memory in the Java language, stack memory mainly holds the following: basic data types and object references, while heap memory stores objects, stack memory faster than heap memory. summed up in a sentence is: referencing the stack while the object is in the heap.
There are two comparisons in Java, the = = and Equals () methods, Equals () is the method of the object class, and the Equals () method defined in the object class is implemented as follows:

public boolean equals (Object obj) {return (this==obj);}

The string class overrides the Equals () method, altering the principle that these types of objects are equal, that is, the principle of determining whether an object is equal depends on whether the contents of the two are equal.
To understand the above, we say that the essence of the String,string class is the character array char[], followed by the string class is final, is not inheritable, this may be ignored by most people, again string is a special type of encapsulation, using string can be directly assigned value , you can also create objects with new, but the implementation mechanism is different. There is also the concept of a string pool in which the Java runtime maintains a string pool in which string objects are not duplicated, created, or dropped. The string pool does not belong to heaps and stacks, but to constant pools. The real meaning of the above code is analyzed below

String str = "ABC"; String str1= "ABC";

The real meaning of the first sentence is to create an object "ABC" in the string pool, and then reference when Str points to the object in the pool "ABC". The second sentence executes, because "ABC" already exists in the string pool, so it is no longer created, then Str==str1 returns True to understand. str1== "ABC" must be correct, there is only one "ABC" in the string pool, and STR and str1 all point to "ABC" in the pool, that's the truth.

String str2 = new String ("abc");

This is a hot issue for Java SE, which is known to create 2 string objects in a single sentence, and based on the above two sentences, create a str2 reference in the stack memory only, create a string object on the heap memory, the content is "ABC", and Str2 point to the first address of the heap memory object.  
    The following is str2== "abc" problem, obviously not, "ABC" is the object in the string pool, and str2 points to the heap memory of the string object, = = To determine the address, must not wait. &NBSP,
    str1.equals (str2), this is right, as stated earlier, the equals of the string class overrides the Equals () method of the object class, actually determining whether the content is the same.  
    Below is the Intern () method, which, in the Javadoc document, describes the Intern () method: Returns a normalized representation of a string object. How do you understand this sentence? The process is actually doing this: The method now looks in the string pool for an object, and there is a reference to the object in the string pool that is returned.  
    In this case, the string pool exists "ABC", then The Intern () method is called when the "ABC" object reference in the pool is returned, then the STR/STR1 is identical, and the str2 is different. Because str2 points to heap memory.   the
    hashcode () method is a hash code that returns the contents of a string, since the contents are identical and the hash code is necessarily the same, and they are equal, which is easy to understand.  
Look at the following example:  

public class Test {private static String str = "abc";p ublic static void Main (string[] args) {String str1 = "a"; String str2 = "BC"; String Combo = str1 + str2; System.out.println (str = = combo); System.out.println (str = = Combo.intern ());}}

This example is used to illustrate that when using the + connection string, the object is actually created in the heap content, then combo points to the space header address of the heap memory store "ABC" string, obviously Str==combo is wrong, and str==combo.intern () is correct, There is also "ABC" in the string pool, which returns directly, and Str also points to the "ABC" object in the string pool. This example shows that any re-modification of the string is a reallocation of memory space, which makes the string objects non-intrusive. That is, the contents of the string once the build is immutable, until a new object is generated.
At the same time the problem comes, using the + connection string to generate new objects each time, and is on the heap memory, and heap memory speed is relatively slow (relative), then a large number of connection strings directly + is not desirable, of course, a high-efficiency method is required. Java provides stringbuffer and StringBuilder to solve this problem. The difference is that the former is thread safe and the latter is non-thread safe, StringBuilder after JDK1.5. There is no guarantee that safe StringBuilder have higher efficiency than stringbuffer.
Since JDK1.5, when the Java virtual machine executes the string's + operation, the internal implementation is also StringBuilder, previously implemented with StringBuffer.
Welcome to communicate, hope to be useful to users.

Original address: Sarin's blog--http://sarin.iteye.com/blog/603684/

I haven't thought about what C # is ...

Classic question for "Go" Java String Object (new String ())

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.