Java basic data type memory allocation

Source: Internet
Author: User

1.java programs run with 6 of places to store data, namely: register, Stack, heap, static storage, constant storage, non-RAM (random memory), mainly heap and stack storage.   2. Heap and Stack is where Java stores data in RAM, Java automatically manages heaps and stacks, and programmers cannot directly set up heaps and stacks. The advantage of the   3 stack is that the access speed is faster than the heap, second only to the registers directly in the CPU, and the stack data can be shared.   But the downside is that the size and lifecycle of the data in the stack must be deterministic and inflexible.  4. The advantage of heap is that it can dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, the Java garbage collector will automatically take away these unused data, the disadvantage is because the runtime to allocate memory dynamically, the access speed is slow.  5. Basic data type storage, Java has 8 types of basic data types: Int,short,byte,long,float,double,boolean, (There is no basic type of string in the base data type). This type is defined in the form of int=3, called an automatic variable. An automatic variable exists as a literal value, that is, not an instance of a class, or a reference to a class. A is a reference to the int type, pointing to the literal value of 3. Because of the size of the data, the lifetime is known (these values are fixed in a program block, the program block back, the value of the field disappears), the reason for the pursuit of speed exists in the stack.   6. The other stack has a very important particularity, that is, the data in the stack can be shared. If you need to define an int a = 3; int b = 3; These two automatic variables. The compiler processes int  a=3; first creates a reference to a variable in the stack, and then looks for a stack with a reference to a value of 3, and does not find an address that holds the literal value of 3, and then points A to the 3 address. Then deal with int b=3, after creating the reference variable of B, because it already has a value of 3 in the stack, it will point to the address of 3 directly. In this case, A and B colleagues point to 3. After defining A and B, in order to a=4, then B will not be equal to 4, but equal to 3. Inside the compiler, when it encounters, it will be in the new search stack if there is 4 of this literal value, if not, re-open the address to store 4 of values. If there is already a direct point A to this address, so the value of a change does not affect the value of B.   7. The memory model of the object. Creating an object consists of declaring and instantiating an object in two steps: Declaring the object's reference and instantiating the object. When declaring an object reference, the stack memory is allocated space for the reference variable of the object, and the object is instantiated as a class member variable in heap memory.Allocates memory, initializes it to the default values for each data type, then displays the initialization, and finally calls the constructor method to assign a value to the member variable, returns a reference to the object in the heap memory (the equivalent of the first address) to the application variable, and references the object in the heap memory by referencing the variable.   8. Storage of wrapper data: the definition of the basic data type is directly on the stack, and if the wrapper type creates the object, it is the same as the normal object. The   9.string data type is a special data type that can be created either in the basic data type format or in a common base type.

Java basic data type memory allocation

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.