Differences between basic data types and reference data types for Java

Source: Internet
Author: User

Reference data types: class, interface type, array type, enumeration type, annotation type, basic data type and reference data type differences:

When the base data type is created, it divides the memory on the stack and stores the value directly on the stack;

When a reference data type is created, it first allocates a piece of memory to its reference (handle) on the stack, and the object's specific information is stored on the heap memory, and then the reference above the stack points to the address of the object in the heap.

For example, there is a class person, with attribute Name,age, with the constructor method of the argument,

Person p = new person ("Tom", 20);

The specific creation process in memory is:

1. First in the stack memory bit its p allocated a space;

2. Allocate a block of space to the person object in heap memory and set the initial value "" for its three attributes, 0;

3. According to the definition of the property in the class person, the object's two attributes are assigned operation;

4. Call the construction method and assign a value of two to "Tom", 20; (Note that there is no connection between p and person objects at this time);

5. Assign the address of the person object in the heap memory to p in the stack, and use the handle p to find specific information about the objects in the heap

Differences between basic data types and reference data types for Java

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.