Java basic data type memory allocation

Source: Internet
Author: User
Tags wrapper
1.java programs run with 6 of local storage data, respectively: registers, stacks, heaps, static storage, constant storage, non-RAM (random memory), mainly heap and stack storage.    2. Stacks and stacks are places where Java is used to store data in RAM, Java automatically manages heaps and stacks, and programmers cannot directly set up heaps and stacks.    3. The advantage of the stack is that the access rate is faster than the heap, second only to the register directly in the CPU; Stack data can be shared.   But the downside is that the data size and lifecycle in the stack must be fixed and inflexible.  4. The advantage of the heap is that the memory can be dynamically allocated, the lifetime does not need to tell the compiler beforehand, the Java garbage collector will automatically take away these no longer used data, the disadvantage is due to the dynamic allocation of memory at run time, access speed is slow.  5. Storage of basic data types, Java has a total of 8 basic data types: Int,short,byte,long,float,double,boolean, (basic data types do not have a basic type of string). This type is defined in the form of int=3, called an automatic variable. An automatic variable has a literal value that is not an instance of a class, nor is it a reference to a class. A is a reference to the type int, pointing to the literal value of 3. These literal data due to size know, lifetime known (these literals are fixed in a program block, the program block back, the field value disappears), in the pursuit of the reasons for the speed of the stack.    6. Another stack has a very important particularity, is the existence of the stack data can be shared. If you want to define 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 finds a reference to the stack with a literal value of 3, fails to find it, opens an address that holds the literal value of 3, and then points A to the address of 3. Then the int b=3 is processed, and after the B-reference variable is created, since the literal value of 3 is already in the stack, the B directly points to the 3 address. In this way, A and B colleagues point to 3. After the definition of A and B, in order to a=4, then B is not equal to 4, but is equal to 3. Inside the compiler, it will have a value of 4 in the new search stack, and if not, reopen the address for 4. If you have already directed a to this address, a value change does not affect the value of B.    7. object's memory model. Create an object that includes the declaration and instantiation of the object: declaring the reference to the object and instantiating the object. When declaring an object reference, allocate space in the stack memory as the reference variable for the object; The object instantiation is, in the heap memoryAllocates memory for class member variables in the it is initialized to each data type default value, followed by a display initialization, and finally the constructor method is assigned to a member variable, which returns a reference to the object in the heap memory (equivalent to the first address) to the application variable, referencing the object in the heap memory by referencing the variable.    8. Storage of wrapper class data: The basic data types are defined directly on the stack, and if the wrapper type is created, the object is the same as the normal object.    9.string data type is a special type of data that can be created either in basic data type format or in common base types.

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.