Understanding reference passing and value passing in Java

Source: Internet
Author: User

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.

>> saving of basic types and reference types in memory

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.

differences between the basic and reference types of >> variables

The basic data type allocates space to the system when it is declared:

The reference is different, and it declares that only the variable is assigned a reference space and no data space is allocated:

Date date;//executes the instantiation, opens up the data space to hold the date object, and then passes the first address of the space to the Today variable  //date=new date ();//If you comment out the previous action//the local variable date may Not having been initialized//means that the object's data space is not assigned date.getdate ();

  

Take a look at the following initialization procedure, Note that "references" are also space-consuming, and the reference size of an empty object object is about 4byte:

Date A, B; Open two reference space in memory A = new date ();//Open the data space for storing the Date object and assign the first address of the space to a B = A; Writes the address in the a storage space to the storage space of B
>> 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 and formal parameters, The actions in the subsequent methods are modifications to the value of the parameter, without affecting the value of the actual argument .

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 are the same as the arguments, and the operation of the formal parameter is actually the operation of the actual parameters, and the change of the parameter value in the method execution will affect the actual parameter the value of the number .

See an example:

class Myobj{public int b=99;}

pass int and object type separately:

public class ReferencePkValue2 {public static void main (string[] args) {  ReferencePkValue2 t = new ReferencePkValue2 () ;  int a=99;  T.test1 (a);//The parameter a passed here is passed by value  System.out.println (a); MYOBJ obj=new MyObj ();  T.test2 (obj);//The parameter passed here, obj, is the reference pass SYSTEM.OUT.PRINTLN (OBJ.B);}  public void test1 (int a) {  a=a++; System.out.println (a);}  public void Test2 (MyObj obj) {  obj.b=100; System.out.println (OBJ.B);} }

The output is:
99
99
100
100

As you can see, the int value does not change, but the changes made to the Obj class in the Test2 method affect the Obj object.

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:

public class ReferencePkValue1 {public static void main (string[] args) {ReferencePkValue1 pk=new ReferencePkValue1 ();// String similar to the base type, value passed, does not change the value of the actual parameter string test1= "Hello";p k.change (test1); System.out.println (test1);//stringbuffer and StringBuilder, etc. are quoted as passing StringBuffer test2=new stringbuffer ("Hello"); Pk.change (TEST2); System.out.println (Test2.tostring ());} public void Change (String str) {str=str+ ' World ';} public void Change (StringBuffer str) {str.append ("World");}}

The output is:
Hello
Helloworld
The operation of string and StringBuffer produces different results.

>> 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) The reference type is quoted, and the modification of the parameters affects the actual parameters;
(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.

Understanding reference passing and value passing 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.