Reference types and value types in Java

Source: Internet
Author: User

Difference between value types and reference types

Custom

A reference type indicates that the data you are manipulating is the same, that is, when you pass a parameter to another method and you change the value of the variable in another method, the value of the passed variable will change when you call the method.

A value type means copying a current variable to a method, and when you change the value of the variable in this method, the value of the variable that was originally declared does not change.

"Value type"

The value type is the basic data type the basic data type is often referred to as four types of eight

    1. Integer: Byte, short, int, long
    2. Float type: float, double
    3. Character type: Char
    4. Logical Type: Boolean

"Reference type"

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

[Value passing]

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.

After figuring out the value type and the reference type, the last point is that value passing and reference passing

"Reference delivery"

Assignment between reference types belongs to reference passing. A reference pass is a reference address of an object, that is, its own

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.

The reference is similar to our pointer, but it does not require us to go to the specific operation

"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 you access a data with a reference type, you need to check the contents of the variable into the stack,

The variable 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.

2, why is there a heap and stack of data stored in Java?

Reference types and value types in Java

Related Article

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.