Differences between value types and reference types in Java

Source: Internet
Author: User

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.

(1)
" value 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 ( 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.

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

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 is recycled only if an object does not have any reference to the variable reference.

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.

 Public Static voidMain (string[] args) {String str= "a"; StringBuffer SB=NewStringBuffer ("a"); Process (str); System.out.println (str); Process (SB); System.out.println (SB); Process (STR,SB); System.out.println (str); System.out.println (SB); //result a a a AA} Public Static voidprocess (String str) {str= "A";} Public Static voidprocess (StringBuffer sb) {SB=NewStringBuffer (); Sb.append (A);} Public Static voidprocess (String str, stringbuffer sb) {str=NewString ("A"); Sb.append (A);} Question: String,stringbuffer is not all reference types, why does the code run with a a a a aA (no change)? 

If you think that a reference type changes values within a function to reflect the outside, it only means that you don't really understand the reference
The first process, local variable assignment, which only changes the point of the local variable, and does not change the contents of the string originally pointed to

The second process, the same problem, assigns a value to the local variable, SB = new StringBuffer (), and the next SB is not the external stringbuffer, so whatever you do is not reflected in the external

The third process,str = new String ("a"), and the previous two is the same as the problem, while the Sb.append ("a") is valid, so you see the last output of AA.

The parameters in the method are local variables, take the last one, the method public static void process (String str, stringbuffer SB) in the parameters str and SB are local variables, call Process (STR,SB),

is to have the local variable point to the same object as the passed-in parameter, str = new String ("A"); just point the local variable str to another object reference, which is not related to the external str . so str is still a, and Sb.append ("a");

is to append "A" to the object that the local variable SB points to, and because the local variable points to the same object as the external SB, the external SB becomes AA.

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.