Differences between value types and reference types in Java

Source: Internet
Author: User

Custom

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

Then calling this method is the value of the passed variable will also change. A value type means that a current variable is copied to the method,

When you change the value of this variable in this method, the value of the initial life variable will not change. Popular parlance: The value type is cash, must use directly, the reference type is the Passbook, must use also must first go to the bank withdrawal. ----(excerpt from online)

[Value type]

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

Four categories:

1, Integer 2, float type 3, character 4, logic type

Eight kinds:

1, integral type 3 kinds of Byte,short,int,long

2, Float type 2 kinds of float,double

3, character type 1 char

4, Logic type 1 Boolean

[Reference type]

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

After figuring out the value type and the reference type, the last point is that the value is passed and the reference is passed, which is the key

[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.

[Citation Pass]

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.

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 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 with reference types, when the method ends, 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. For example:

String S=new string ("person") and s=new string ("man"); S is the object that points to the value of person in the heap memory, but s suddenly hates the person, which points to the man object in the heap memory. The person is abandoned like an orphan, and only the garbage collector can find it, and the garbage collector mercilessly cleans up the person as garbage and destroys it ruthlessly to free up memory.

Differences between 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.