Differences in string, StringBuffer, and StringBuilder in Java and stack memory allocations

Source: Internet
Author: User

The string class in Java is a very common, but not the least, of the details of the class, so most interviews will be that kind of fuss. For example, string str = new string ("Hello"), opens up several memory spaces, the difference between string and StringBuffer, and so on. Here's what I understand:

A string is a final-modified class that cannot be inherited. StringBuffer is also the final modified class.

One, heap memory and stack memory

In the JVM, heap memory is the memory space where the object is instantiated (the data of the program), the stack memory is the name of the object, and its content is the address of the corresponding heap.

It is also possible to say that all object names are stored in the stack memory, the object content remains in heap memory, and the reference type data must use the New keyword to open up space in heap memory.

Second, sring memory allocation

String has a special place: You can construct a string object by using the new construct or by using "hello" directly. The second option is recommended for both methods.

1, String a = "Hello";

2, String a= new string ("Hello");

The explanations are as follows:

1: An A object reference is defined in the stack memory, pointing to the value of the heap memory "Hello" memory address. Finally opens up a memory space

2: A reference to the A object is redefined in the stack memory, pointing to the heap memory value as the "Hello" memory address, and then to the address where heap memory is "hello" after new. Finally, two spaces are opened, and the first space has no object references and is garbage collected by the JVM.

This is illustrated below:

Understanding the above is not difficult to understand the following code:

Package andy.string.test;/**   * @author zhang,tianyou   * version:2014-11-25 pm 4:15:14   *  * *   * public class TestString {public static void main (string[] args) {String a = "Hello"; String b = "Hello"; String c = new string ("Hello"); String d = new String ();d = "Hello"; String e = c; System.out.println ("a==b" + (a== b)); System.out.println ("A==c" + (c== b)); System.out.println ("A==d" + (a== D)); System.out.println ("a==e" + (a== e)); System.out.println ("C==d" + (c== D)); System.out.println ("c==e" + (c== e));}}

Only A==b==d, c=e.

Explain:

1, the string every new heap of memory do not want to wait, and D after new allocation of newly assigned address, and then discard new address, pointing to a corresponding memory address, so they are the same.

2. The "Hello" assignment is the same as the heap memory space pointed to by the direct assignment method. String uses a shared design in Java to form a pool of objects in Java that can hold multiple objects, and if the newly instantiated objects are already present in the object pool, they are not duplicated and are removed directly from the object pool. So

For content that already exists, the object points to the space address of the instance.

3, E directly points to the memory space of C.

4, so when using string, it is recommended to use direct assignment, reduce memory space, improve performance.


Iii. differences between String, StringBuffer, and StringBuilder

1, String, StringBuffer, StringBuilder are final modified, is not able to be inherited rewrite.

2, string after instantiation, the content size of its memory space can not be modified, and StringBuffer is a thread-safe variable character sequence, after instantiation can dynamically modify the contents of the heap memory, so the memory length and size is variable The memory size length after StringBuilder instantiation is also variable, not

The same thing is that StringBuilder is not thread-synchronous, so it's necessarily more efficient to operate than stringbuffer.

This is someone who will want to:

String str = "Hello";

str + = "Andy";

Doesn't the value of str change?

In fact, the above code in memory has opened up 3 space, respectively: "Hello", "Andy", "Helloandy", their heap memory size is fixed, and finally str points to the "Helloandy" heap address. As shown in the following:

When StringBuffer is used, it will only open up a memory space, you can use append to add operations such as delete.

Each time a String generates an object, it has an impact on the system performance, especially when there are no more reference objects in memory, and the JVM's GC will begin to work, which will certainly be quite slow. If you use the Stringbuffer/stringbuilder class, the result will be different, and each result will operate on the Stringbuffer/stringbuilder object itself, instead of generating a new object and changing the object reference.

therefore, when assigning a value to a string, it is best to use StringBuffer (thread-safe) or StringBuilder, which can save memory, improve performance, and remember .



Differences in string, StringBuffer, and StringBuilder in Java and stack memory allocations

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.