Cash and passbook --- value type and reference type

Source: Internet
Author: User

I also touched on the Value Type and reference type during the soft exam. At that time, I was able to cope with the questions. However, after the test, I suddenly faced these two words and felt confused. Now let's think about it. At that time, I simply understood its principle and didn't use code to implement it. So I can only get a preliminary understanding of it for the time being. This article aims to make up for the preliminary regret and further study.

Linking theory with practice is beyond reality.Just like a door and a key, a perfect combination can protect against theft and security. Therefore, the main idea of this article is to elaborate on "value type and reference type" in both theory and practice.


-------------------------------------------Theory------------------------------------

1.Value Type

Data Types of resources automatically released by the system when the memory is directly stored in the stack. To put it simply:The value type is like cash.And can be used directly when needed;

2.Reference Type

The data type referenced by the actual value of the type. To put it simply:The reference type is like passbook.When necessary, you must first switch to the bank for cash and then use it.

 3.Comparison between the two

Use a simple chart to make it more intuitive and easy to understand.


 

-------------------------------------------Practice------------------------------------

After reading it, you will have a rough understanding of the value type and reference type. The following describes in detailMemory Allocation differences.

As shown in the table above,Value TypeThe variable is inStack memory,Reference TypeInHeap memory. What does that mean? Let's take a look at the following code example and memory distribution diagram.

// First, construct a simplest class reference type publicclass myclass {}// then, compare the value type and reference type Private Static void main () {int I; // define the value type variable myclass MC; // define the reference type variable I = 5; // The value of the Value Type Variable MC = new myclass (); // create the reference type variable}

This is a simple piece of code. Let's take this example and use the memory distribution diagram to compare the differences between the value type and the reference type in memory allocation.

First, theyCommon startThat is, as long as it is declared as a variable (whether it is a value type or a reference type variable), it will allocate a part of its space in the stack memory. For example:

As for what is stored in this space, it depends on whether the variable is of the value type or reference type.


(1)Value Type

I = 5; // Value Type Variable assignment

When a value type variable is assigned,Stack memoryThis is what is stored inValue of the value type itself. As shown in:

As shown in the figure, the memory occupied by the value type variable itself stores its value, just like the "cash" mentioned above. It is easy to use it directly when needed, one step is in place.


(2)Reference Type

MC = new myclass (); // create a reference type variable

When a variable of the reference type is usedStack memoryTo allocate space for it, you must alsoHeap memoryAllocate appropriate space for it. However, you must note that what is stored in the stack memory? What is stored in heap memory? Let's take a look at the figure:

The figure shows that the value of the reference type variable is stored in the heap memory, and the value in the stack memory is the first address corresponding to the variable in the heap memory, just like a passbook, the stack stores only the passbook account. To use it, we also need to find the corresponding "cash" in the heap memory based on the "Account" in the stack ", before you can use it.

 

I don't know. Do readers understand the difference between the value type and the reference type? Especially in terms of memory allocation, this is essentially different. We look forward to hearing from 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.