Difference Between Stack and heap and the internal working mechanism of string variable assignment in Java)

Source: Internet
Author: User
Difference Between Stack and heap and the internal working mechanism of string variable assignment in Java)

1. Both stack and heap are places where Java is used to store data in Ram.
2. The advantage of stack is that the access speed is faster than the heap speed, second only to the registers directly located in the CPU. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. In addition, stack data can be shared. For details, refer to the 3rd point. The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told to the compiler in advance. The Java garbage collector will automatically collect the data that is no longer used. However, the slow access speed is due to the need to dynamically allocate memory during runtime.
3. There are two data types in Java. Basic Types (primitive types): int, short, long, byte, float, double, Boolean, and char. Exist in the stack. The other is the packaging class data, such as integer, String, double, and so on. All these types of data exist in the heap.

String STR = "ABC"; and string STR = new string ("ABC"); and char [] C = {'A', 'B', 'C '}; string STR = new string (c); both use Heap Storage

String STR = "ABC"; in the stack, if there is no address with the value of "ABC", it is equivalent:
String temp = new string ("ABC ");
String STR = temp;

About the internal work of string STR = "ABC. Java converts this statement into the following steps:
(1) first define a reference variable for the string class object named STR: String STR;
(2) check whether there is an address with the "ABC" storage value in the stack. If not, open an address with the "ABC" Storage name, create a new string class Object o, point the O string value to this address, and write down the referenced object o next to this address in the stack. If an address with a value of "ABC" already exists, search for the object O and return the o address.
(3) Point STR to the address of object o.

The string STR = "ABC"; method can improve the running speed of the program to a certain extent, because the JVM automatically determines whether it is necessary to create a new object based on the actual data in the stack. For the code of string STR = new string ("ABC");, a new object is created in the heap, regardless of whether the string value is equal, whether it is necessary to create a new object, this increases the burden on the program.

Char [] C = {'A', 'B', 'C'}; string STR = new string (c); equivalent:
String STR = new string ('A' + 'B' + 'C ');
**************************************** **************************************** ***
* Tip: http://community.csdn.net/Expert/topic/4848/4848360.xml? * Temp = 9.325808e-02
* After reading it, I really understood it. Thank you! Share with you !!
**************************************** **************************************** ***

 

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.