JAVA differences between value types and reference types

Source: Internet
Author: User


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

The [definition] 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 calling this method is the value of the passed variable will also change. 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, must use directly, the reference type is the Passbook, must first go to the bank to withdraw

[Value type] that is, the basic data type is commonly referred to as four classes of eight kinds of four classes: 1, Integer 2, float 3, character 4, logic eight kinds: 1, integer 3 species Byte,short,int,long 2, floating-point 2 species Float,do Uble 3, character 1 char 4, logic 1 boolean[Reference type] All types are referred to as reference types (arrays, classes, interfaces, strings) except four classes of eight basic types.

After figuring out the value type and the reference type, the last point is the value pass and the reference pass, which is the key [value pass] The base data type assignment is the value pass, the value passed is a real variable value, is a copy of the original parameter, the value is passed, the argument is passed to the value of the parameter, the parameter changes without affecting the

The assignment between reference types is a reference pass. 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 (value) 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 reclaims 2 only if an object does not have any reference variable references. Why is there a heap and stack of data stored in Java?

JAVA differences between value types and reference types

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.