Java reference passing and value passing

Source: Internet
Author: User
Tags stringbuffer java reference

It is always a topic to talk about whether a reference pass or value is passed on a Java parameter.
There is a forum that there is only value in Java, there are some places that reference passing and value delivery are present, it is easier to confuse people.
About value passing and reference passing actually need to look at the situation, today study and analysis, anxious can first look at the final conclusion.

1. Saving in memory for basic types and reference types

Data types in Java fall into two categories, basic types and object types . Accordingly, there are two types of variables: the base type and the reference type.
A variable of a primitive type holds the original value, that is, the value it represents is the value itself;
Whereas a variable of a reference type holds a reference value, the reference value points to the address of the memory space, which represents the reference to an object, not the object itself.
The object itself is stored at the location of the address represented by the reference value.

Basic types include: Byte,short,int,long,char,float,double,boolean,returnaddress,
Reference types include: Class types, interface types, and arrays.

Accordingly, there are two types of variables: the base type and the reference type.

2. Reference passing and value passing

The concept of actual parameters and formal parameters is used here to help understand,

Value passing:

When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the function receives a copy of the original value, at which time there are two equal basic types in memory , that is, the actual parameter and the formal parameter, and the operation in the subsequent method is the modification of the value of the parameter. Does not affect the value of the actual parameter .

Reference delivery:

Also known as a pass-through address. When a method is called, the actual argument's reference (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and the function receives the memory address of the original value;
In the execution of the method, the parameters and the arguments are the same, pointing to the same memory address, and the operation of the reference in the method execution will affect the actual object .

See an example:

1  Public Static voidAinta) {2a++;3 System.out.println (a);4     }5 6      Public Static voidA (MyInt a) {7a.a++;8 System.out.println (A.A);9     }Ten  One     classMyInt { A         intA = 1; -     } -  the      Public Static voidMain (string[] args) { -         intA = 1; -MyInt B =NewTest ().NewmyInt (); - A (a); + System.out.println (a); - A (b); + System.out.println (B.A); A}

The output here is: 2,1,2,2. The value of the INT type variable is not changed here, and the value of the class object is changed, the former is the value pass, and the latter is the reference pass.

Here special consideration is given to string, and several basic types, such as Integer, double, are immutable types,
Because there is no function to provide its own modification, each operation is a new object, so special treatment, it can be considered to be similar to the basic data type, transfer value operation.

Look at the following example:

1  Public Static voidChange (String a) {2a+= "World";3     }4     5      Public Static voidChange (StringBuffer a) {6A.append ("World");7     }8     9      Public Static voidChange (StringBuilder a) {TenA.append ("World"); One     } A      -      Public Static voidMain (string[] args) { -String a= "Hello"; theStringBuffer b=NewStringBuffer ("Hello"); -StringBuilder c=NewStringBuilder ("Hello"); - Change (a); - System.out.println (a); + Change (b); - System.out.println (b); + Change (c); A System.out.println (c); at}

The result of the output here is Hello,helloworld,helloworld. Thus, a string is a regenerated object, Stringbuff and Stingbuilder are reference passes

3. Conclusion

In conjunction with the above analysis, the conclusion about value passing and reference passing can be drawn as follows:

(1) The basic data type transmission value, the modification of the formal parameters will not affect the actual parameters;
(2) Reference type is referenced, formal parameters and arguments point to the same memory address (the same object), so the modification of parameters will affect the actual object;
(3) String, Integer, double, etc. immutable type special processing, can be understood as the value of the pass, the last operation will not modify the argument object.

Transfer from https://www.cnblogs.com/binyue/p/3862276.html slightly modified

Java reference passing and value passing

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.