difference between a value class and a reference class

Source: Internet
Author: User

The difference between a value type and a reference type is that a variable of a value type directly holds the actual data, whereas a variable of a reference type holds the address of the data, that is, the object's reference.
A value type variable stores the value of a variable directly on the stack, and a variable of the reference type holds the address of the actual data on the stack, while the actual data is stored in the heap. Note that heap and stack are two different concepts, where storage is not the same in memory, heaps are typically used to store variable-length data, such as string types, while stacks are used to store fixed-length data, such as Integer type data int (each int variable occupies four bytes). Where the data is stored, it is known that when assigning a value variable to another value variable, two identical values are saved on the stack, and a reference variable is assigned to another reference variable, and two references to the same heap location are saved on the stack, that is, the address of the same heap is saved on the stack. In the case of data manipulation, for value types, because each variable has its own value, the operation of one variable does not affect other variables, and for a variable of reference type, manipulating the data of a variable is the operation of the data in the heap, if a variable of two reference types refers to the same object, The actual meaning is that they hold the same address of the heap on the stack, so manipulating one variable affects another variable that references the same object.

difference between a value class and a reference class

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.