Java Beginner (1): The difference between a value type and a reference type

Source: Internet
Author: User

differences between Java value types and reference types (GO)                                                          

[ 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 passed variable is changed by calling this 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 initial life variable does not change.
Popular parlance : The value type is cash, to use directly, the reference type is a passbook, you have to go to the bank first to take cash. ----(excerpt from online)
[ value type]: à That is, the basic data type

The basic data types are often referred to as four types of eight

Four categories: 1, Integer 2, float type 3, character 4, logic type

Eight kinds: 1, 3 kinds of Byte,short,int,long type

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

[ garbage collection mechanism in Java ]

When an object in a heap memory is not "referenced" by the value in the stack memory that represents the address, the object is called a garbage object, it cannot be used but occupies an area in memory, as in this case:

string s = new string ("person"); s = new String ("Man"); S is supposed to point to the object of the heap in the memory of the person, but s suddenly hate the person, it points to the heap in the memory of the man object, the person is like an orphan was abandoned by S, but the person is more miserable than the orphan, because there is nothing to find it, in addition to the high weight of the ' Garbage collector ', but it's not always good to be found by officials, especially the ' garbage collector ', which will take ' junk ' away and ruthlessly destroy it to free up memory.

[ box packing and unpacking ]

In fact, boxing is the conversion process of a value type to a reference type. Boxing a value-type variable into a reference-type variable first allocates memory space on the managed heap for the new reference-type variable, then copies the value-type variable to the newly allocated object memory on the managed heap, and finally returns the newly allocated object memory address. The boxing operation is reversible, so there is a unpacking operation. The unboxing operation gets a pointer to the object that contains the value type part, and the programmer manually copies its corresponding value to the value type variable

Java Beginner (1): The difference between a value type and a reference type

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.