String S=new string ("xyz"), problem with creating several string objects

Source: Internet
Author: User

First let's look at a few concepts:

Stack: An area allocated by the JVM to hold actions and data references that are executed by the thread. Heap: An area allocated by the JVM for storing data such as objects. Constant pool constant pool: a chunk of storage allocated in the heap for storing explicit string,float or integers. This is a special shared area that can be shared in memory without changing things that can be put here. Get to the point: String a = "abc"; ①
String b = "abc"; ② uses string a = "abc", which can improve the speed of the program to a certain extent, because the JVM will automatically determine whether it is necessary to create new objects based on the actual facts of the data in the stack.
① code executes a string object with a value of ABC in constant pool, ② executes because "ABC" exists in the constant pool and is not creating a new string object. String c = new String ("xyz"); ①
String d = new String ("xyz"); ② let's see what happens in memory with these two lines of code, and when ①class is ClassLoader loaded, your "XYZ" is read as a constant, creating a shared "in the constant pool" XYZ ", and then when called to new string (" XYZ "), the new string (" XYZ ") is created in the heap; ② no longer creates" XYZ "because of the presence of" XYZ "in the constant pool, and then creates a fresh string ( "XYZ").
for string c = new String ("XYZ"), the code, unlike string a = "abc", is to create new objects in the heap, regardless of whether their string values are equal, and whether it is necessary to create new objects, thereby aggravating the burden on the program.
Procedure 1
string S1 = new String ("XYZ"); Create two objects, one reference
String s2 = new String ("XYZ"); Creates an object, and every subsequent execution creates an object, a reference
Procedure 2
String s3 = "XYZ"; Create an object, a reference
String S4 = "XYZ"; Do not create an object, just create a new reference it is important to understand the constant pool and the new keyword when the Intern method is called, if the pool already contains a string equal to this string object (the object is determined by the Equals (object) method), The string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned. (regardless of how all the objects in the pool are returned) The following example can help us to further understand the storage and assignment principle of string str1 = new String ("123");
String str2 = "123";

String STR3 = Str1.intern ();

System.out.println ((str1 = = str2) + "," + (STR3 = = str2)); Output false,true

String STR4 = new String ("234");
String STR5 = new String ("234");

String STR6 = Str4.intern ();
String STR7 = Str5.intern ();


System.out.println ((STR4 = = STR5) + "," + (STR6 = = STR7)); Output false,true

String S=new string ("xyz"), problem with creating several string objects

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.