String a=
"a"
+
"b"
+
"c" 创建了几个对象
This problem involves string constant pooling and string concatenation .
String a= "A" + "B" + "C"
After the compiler is optimized, the effect is
String a= "ABC"
There are two forms of string object creation in Java, one for literal form , such asString str = "droid";
The other is to use the new standard method of constructing objects, such asString str = new String("droid");
Both of these methods are often used in code writing, especially in the literal way. However, there are some differences in performance and memory consumption between the two implementations. It all started with the JVM. In order to reduce the duplication of string objects, it maintains a special memory, which is a string constant pool or string literal pool .
When we use new to construct a string object, the new string object is created (in the heap), regardless of whether there are objects of the same content in the string constant pool.
Heap : Store all new objects;
Stack : The application of the variable data and objects of the basic type , the object (new object) does not exist in the stack itself, but is stored in the heap or in a constant pool (the string constant object is stored in a constant pool);
constant Pool : holds basic type constants and string constants.
objects in the stack and constant pools can be shared, and objects in the heap cannot be shared. The data size and life cycle in the stack can be determined, and the data disappears automatically when no reference is directed to the data. The garbage collector is responsible for the collection of objects in the heap, so the size and life cycle do not need to be determined and have great flexibility.
For strings, references to objects are stored in the stack, and if the compilation period has been created (that is, defined in double quotes), it is stored in a constant pool, and if the runtime (the new object) is stored in the heap. For strings equal to equals, there is only one copy in the constant pool and multiple copies in the heap.
Two deep-reading links
string constant pool:/ httpdroidyue.com/blog/2014/12/21/string-literal-pool-in-java/
String stitching internal implementation:/httpdroidyue.com/blog/2014/08/30/java-details-string-concatenation/
This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1733249
string constant pool, heap, stack