String Constants pool, heap, stack __java/c++

Source: Internet
Author: User
Tags constant


String a= "A" + "B" + "C" creates several objects


This problem involves string constants pooling and string concatenation


String a= "A" + "B" + "C"


After the compiler optimization, the result is

String a= "ABC"


There are two forms of string object creation in Java, one in literal form , such as String str = "Droid";

The other is a standard method of constructing objects using new , such as String str = new String ("Droid");

Both of these methods are often used when code is written, especially in the literal way. However, there are some differences in performance and memory usage between these two implementations. It all stems from the JVM in order to reduce the duplication of string objects, it maintains a special memory, which is either a string constant pool or a string literal pool .


When we use new to construct a string object, a new string object is created (in the heap), regardless of whether there is an object in the string constant pool that has the same content.



heap : To store all new objects;

stacks : storage of basic types of variable data and object applications , objects (new objects) themselves do not exist in the stack, but in the heap or in a constant pool (string constant objects stored in a constant pool);

constant Pool : holds base type constants and string constants.


objects in stacks and constant pools can be shared and cannot be shared for objects in the heap. The data size and lifecycle in the stack can be determined, and when no reference is directed to the data, the data disappears automatically. The objects in the heap are reclaimed by the garbage collector, so the size and lifecycle do not need to be determined and have great flexibility.

For strings, the references to their objects are stored in the stack, and if the compile period has been created (that is, defined in double quotes), it is stored in a constant pool, and the runtime (the new object) is stored in the heap. strings equal to equals are only one in a constant pool, and there are multiple copies in the heap.



Two links to read in depth
string constant pool: http://droidyue.com/blog/2014/12/21/string-literal-pool-in-java/
string concatenation internal implementation: http://droidyue.com/blog/2014/08/30/java-details-string-concatenation/


This article from the "Drip accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1733249

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.