string Problems in Java

Source: Internet
Author: User
Tags java se

Mode one: String a = "AAA";

Mode two: String b = new String ("AAA");

You can create a string object in either of two ways, but one is better than the other way.
Because the string is stored in a constant pool, the object created by new is stored in heap memory.
One: There is already a string constant "AAA" in the constant pool

When you create an object by means of a, the program runs in a constant pool looking for the "AAA" string, assigning the address of the found "AAA" string to a.
Creating an object by means of two, regardless of whether there is a "AAA" string in the constant pool, the program opens up a new space in the heap memory to hold the new object.
One: There is no string constant "AAA" in the constant pool

When you create an object by means of a, the program runs with the "AAA" string in the constant pool and assigns its address to a.
By creating the object in the form two, the program opens up a new space in the heap memory to hold the new object, and puts the "AAA" string in the constant pool, equivalent to creating two objects.
Test:

        String a = "AAA";         = "AAA";         New String ("AAA");                 = = b);        // Compare addresses A, B equals        System.out.println (A = = c);        // Compare address A,c to range        System.out.println (A.equals (c)); // equals Compare content equal

Results:
A==b:true
A==c:false
The values of A and B are equal: true

http://talentluke.iteye.com/blog/1539870

 Public classTest { Public Static voidMain (string[] args) {String str= "ABC"; String str1= "ABC"; String str2=NewString ("ABC"); System.out.println (str= = str1);//trueSystem.out.println (str1 = = "abc");//trueSystem.out.println (str2 = = "abc");//falseSystem.out.println (str1 = = str2);//falseSystem.out.println (Str1.equals (str2));//trueSystem.out.println (str1 = = Str2.intern ());//trueSystem.out.println (str2 = = Str2.intern ());//falseSystem.out.println (str1.hashcode () = = Str2.hashcode ());//true    }  }          

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.

You can assign a value directly when using string, or you can create an object 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.

This is a hot issue for Java SE, as we all know, This sentence creates 2 string objects , and based on the above two sentences, only creates str2 references in the stack memory, Creates a string object on heap memory that is "ABC", while str2 points 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, which is actually determining whether the content is the same.  
The Intern () method, described 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 done this way: The method now looks in the string pool for an object that exists to return a reference to the object in the string pool.  
Then there is "ABC" in the string pool in this case, and the Intern () method is called when the "ABC" object reference in the pool is returned, then the STR/STR1 is identical, and 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 content is identical and the hash code is necessarily the same, then they are equal, which is easy to understand.  

 public  class   Test { private  static  String str = "abc" ;  public  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.

string Problems in Java

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.