Value types and reference types in Java

Source: Internet
Author: User

What is the difference between a value type and a reference type in Java?

1. Definition

A reference type indicates that the data you are manipulating is the same, that is, when you pass a parameter to another method, you change the value of the variable in another method, then the value of the variable passed in is changed, and the value type represents a copy of the current variable to the method, and when you change the value of the variable in this method, The value of the initial life variable will not change. Popular parlance: The value type is the cash, must use directly, the reference type is the Passbook, must use also must first go to the bank withdrawal.

2. Classification

(1) Value type

The value type, which is the basic data type, is often referred to as the four-class eight.

Four categories: 1. Integral type 2. Float Type 3. Character type 4. Logic type

Eight kinds:

1.3 Kinds of integral type: Byte,short,int,long;

2. Floating-point type 2 kinds: float,double

3. Character Type 1 kinds: Char

4.1 Kinds of Logic type: Boolean

(2) Reference type

All types are referred to as reference types ( arrays, classes, interfaces, strings ), except for four classes of eight basic types.

3. Delivery type

(1) Value type delivery

The basic data type assignment is a value pass, the value passed is a real variable value, is a copy of the original parameter is passed, the value is passed, the argument is passed to the value of the parameter, the parameter changes without affecting the argument.

(2) Reference type passing

Assignment between reference types belongs to reference passing. Reference passing is the object's reference address, which is itself (its own most popular understanding).

Reference passing: The address is passed, that is, the address of the argument to the formal parameter, the parameters are changed, and the arguments are changed, because they point to the same address.

4. Memory allocation

A data with a value type is stored in a variable within the stack. That is, allocate memory space in the stack, store the value directly, and its value represents the data itself.

Data of a value type has a faster access speed.

A data with a reference type (reference type) does not reside in the stack, but is stored in the heap. That is, allocating memory space in the heap, not directly storing the contained value, but pointing to the value to be stored, whose value represents the address pointed to. When accessing a data with a reference type, you need to check the contents of the variable into the stack, which refers to an actual data in the heap. Data of a reference type data ratio type has greater storage size and lower access speed.

There is a garbage collection mechanism in Java, and the variables in the stack memory are destroyed naturally with the end memory of the method, and when the method ends with a reference type, the object may be applied by another reference type, not destroyed, and the garbage collection mechanism will be recycled only if an object does not have any reference variable references.

Value types and reference types in 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.