Difference between the value type and the reference type

Source: Internet
Author: User

Difference between the value type and the reference type

[Definition]

The reference type indicates that the data you operate on is the same, that is, when you pass a parameter to another method, you change the value of this variable in another method,

The value of the input variable also changes. The value type indicates copying the current variable to the method,

When you change the value of this variable in this method, the initial value of the variable will not change. in layman's terms, the value type is cash, which should be used directly; the reference type is passbook, and must be obtained from the bank first. ---- (From the Internet)

[Value type]

That is, the basic data type is often referred to as four types and eight types.

Category 4:

1, integer 2, floating point type 3, numeric type 4, logical type

Eight types:

1, three types of integer bytes, short, int, long

2, float type 2 float, double

3, character type 1 char

4. Logic type 1 boolean

[Reference Type]

All types except four categories and eight basic types are referred to as reference types (arrays, classes, interfaces, strings)

After clarifying the value type and reference type, the last point is the value transfer and reference transfer, which is the key

[Value transfer]

Values of the basic data type are transmitted as values. Values are transmitted as real variable values and copies of the original parameters. After the values are transferred, the values of the real parameters are passed to the form parameters, the parameters change without affecting the real parameters.

[Reference transfer]

The value assignment between reference types is passed by reference. The reference transmits the reference address of the object, that is, its own (your most common understanding ).

Transfer by reference: The address is passed to the form parameter. The form parameter is changed, and the real parameter is changed because it points to the same address.

The reference is similar to our pointer, but it does not require any specific operation.

[Memory Allocation]

A data with value type is stored in a variable in the stack. That is, allocate memory space in the stack and store the included values directly. The values represent the data itself.

Data of the value type has a fast access speed.

A data with reference type does not reside in the stack, but is stored in the heap. That is, allocate memory space in the heap. Instead of directly storing the contained values, it points to the value to be stored. The value indicates the address to which it points. When accessing a data with reference type, you need to check the content of the variable in the stack. The variable references an actual data in the heap. Data of the reference type has a larger storage scale and a lower access speed.

Java has a garbage collection mechanism. The variables in the stack memory are naturally destroyed as the method ends. When the reference type is used, when the method ends, this object may be applied by another reference type and will not be destroyed. The garbage collection mechanism will only recycle an object if it does not have any reference variable reference.

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.