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 test. At that time, I was able to cope with the questions, but after the test, I suddenly faced these two words and thought I was confused. Now let's think about it. At that time, I simply understood its principle and didn't implement it using practical code. So I can only get a preliminary and temporary understanding. 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 that are 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 necessary;

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.Memory Allocation differences.

As shown in the table above,Value TypeThe variable is inStack memory,Reference TypeInHeap memory. What does that mean? See the following code example and memory distribution chart.

// First, construct the simplest class reference type publicclass myclass {}// and 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 very easy piece of code. Let's take this example and use the memory distribution graph to compare the differences between the value type and the reference type in memory allocation.

First, theyCommon startThat is, if the variable (whether it is a value type or a reference type variable) is declared, 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 the following figure:

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 very easy to use directly when needed, one step is in place.


(2)Reference Type

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

When it is a reference type variableStack 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 only stores the passbook account. When necessary, we also need to find the corresponding "cash" in the heap memory based on the "Account" in the stack ", before you can use it.

 

Do you understand the difference between the value type and the reference type? This is essentially different, especially in terms of memory allocation. We look forward to hearing from you ~

Cash and passbook --- value type and reference type

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.