Why java is only passed by value?

First programming language if you learn java may not be as sensitive to this transfer mode, if you learn c or c ++, java and then learn, you may be confused on this issue.

1. The value passed to the concept of reference passed

Before passing way to understand what shaped involved in the argument.

In the form of parameters : the parameter is used in the definition of the function names and function of the body when the object is used to receive incoming calls when the function parameters.

Actual parameters: calling the function with a time reference, the data transfer relationship between the calling function and the called function. When you call a function in the calling function, the function name in brackets after the parameter called "actual parameters."

It can be appreciated that: the parameter is a abstract argument, the argument is a call parameter, the parameter is a parameter defined function

Value is passed: method call, passing it the actual parameter values to the corresponding formal parameters, is a function of the received copy of the original value, in which case there are two identical memory basic types , i.e., formal parameters and actual parameters, back the method of operation is to modify the parameter value, it does not affect the value of the actual parameter.

Passed by reference: refers to when calling the function directly address the actual arguments passed to the function, then to modify the parameters carried out, it will affect the actual parameters in functions.

: Draw the focus of the main difference between the transfer value and passed by reference

Value transfer

Passed by reference

Create a copy, you can not change the original value in the function body

Without creating a copy, the original value can not be changed in the function body

 Creating a copy of meaning

Create a copy is to say, the arguments to call out to copy, then copy that a pass into the body of the function. When not creating a copy, copy this step does not occur.

For value passed chestnut:

 1 public class Test01 {
 2     public static void main(String[] args) {
 3         int a=1,b=2;
 4         swap(a, b);
 5         System.out.println("a="+a);
 6         System.out.println("b="+b);
 7     }
 8     public static void swap(int a,int b) {
 9         int temp=a;
10         a=b;
11         b=temp;
12     }
13 }

result:

1 a=1
2 b=2

Chestnut above, so that the function implemented in a, b exchange, but after the function call, the result is still output a, b of the original value, the operation of the function does not affect the body in a, b values ​​in vitro function.

Chestnuts not pass a reference test, there may be interested to test With c ++, defined as an alias for the parameter or pointer, in c ++ is passed by reference.

Myth: parameters passed if it is a common type that is passed by value, and if the object that is passed by reference.

The value passed 2.java

 In java, regardless of the type of parameters are the basic or reference data types, are passed by value. For reference to the following types of data parameters, it has been chestnut above basic data types.

 1 public class Test01 {
 2     public static void  swap(Student st1,Student st2) {
 3         Student temp=st1;
 4         st1=st2;
 5         st2=temp;
 6     }
 7         
 8     public static void main(String[] args) {
 9         Student st1=new Student("张三",20);
10         Student st2=new Student("李四",20);
11         swap(st1, st2);
12         System.out.println("st1:"+st1);
13         System.out.println("st2:"+st2);
14 }

result:

. 1 ST1: Student [name = Joe Smith, Age = 20 is ]
 2 ST2: Student [name = John Doe, age = 20]

Example, the object pointed st1 and st2 is not changed.

At this time, you might ask, since java is passed by value, so real participants copies happen, that copy is what is it? The answer is: a copy of the address of the object is on the heap. To a chestnut to verify:

 

. 1  public  class to Test01 {
 2      public  static  void   Print (Student STU1, Student STU2) {
 . 3          Student TEMP = STU1;
 . 4          STU1 = STU2;
 . 5          STU2 = TEMP;
 . 6          System.out.println ( "after exchange of the function body printing STU1 : "+ STU1);
 . 7          System.out.println (" after exchange of the function body printing STU2: "+ STU2);
 . 8      }
 . 9          
10      public  static  void main (String [] args) {
 . 11          Student = STU1 new new Student ( "STU1", 20 );
12         Student stu2=new Student("stu2",30);
13         print(stu1, stu2);
14     }
15 }

result:

1 after the exchange of the function body printing STU1: Student [name = STU2, Age = 30 ]
 2 after printing exchange function body stu2: Student [name = stu1, age = 20]

As can be seen from the results, in the body of the function and stu2 stu1 object pointed indeed changed, because the copy of their address in the stack during the transfer of value.

Take a look at how their kind in memory:

At this time you may ask, values passed since java is a copy of an address, then I can not just change the contents of the address? The answer is: Of course

1     public static void  changeInf(Student stu) {
2         stu.setName("我改名字了");
3     }
4         
5     public static void main(String[] args) {
6         Student stu=new Student("张三",18);
7         changeInf(stu);
8         System.out.println(stu);
9     }

结果:

1 Student [name=我改名字了, age=18]

结论:java中只有值传递,这可能是因为java没有指针和别名引用的原因吧。

Guess you like

Origin www.cnblogs.com/ironHead-cjj/p/11366888.html