Parametric Passing in Java

Posted by D on Thu, 16 May 2019 21:58:02 +0200

Some time ago, I was a little confused about parameter passing in Java and didn't understand its meaning. Looked up a lot of information, here I talk about my understanding of the problem.

There are two kinds of parameter passing, one is "pass value" and the other is "pass address value". Passing value refers to passing the value of the parameter to the method when calling the method, while passing address value provides the address value of the parameter to the method. Parametric passing methods in Java are called for passing values. Next, I will use an example to verify it.

1. Parameter Passing of Basic Types

 1 public class ParamTransfer {
 2     public int money;
 3     
 4     public void amethod(int i) {
 5         System.out.println("Method obtained i Value:" + i);
 6         i = i * 5;
 7         System.out.println("Method Execution Statement i=i*5 after i Value:" + i);
 8         System.out.println("money Value:" + this.money);
 9     }
10 
11     public static void main(String[] args) {
12         ParamTransfer pt = new ParamTransfer();
13         pt.money = 100;
14         pt.amethod(pt.money);
15     }
16 
17 }
The results are as follows:
The value of i obtained by the method is: 100 The value of i after method execution statement i=i*5: 500 money value: 100

Note the changes in the parameters I and money. Here we pass pt.money as a parameter to the amethod method, where I gets a value of 100, which we can see from the results of the operation. After executing the i=i*5 statement, we find that the value of the money attribute of the current object has not changed, which means that only a 100 value is passed to the parameter i, which is equivalent to copying the value of pt.money to i. The change of I will not affect the value of pt.money, but only the value of pt.money.

2. Parameter Passing of Reference Type

 1 class Time {
 2     public int hour;
 3     public int minute;
 4     public int second;
 5 }
 6 
 7 public class ObjectParamTransfer {
 8     Time time;
 9 
10     public static void main(String[] args) {
11         ObjectParamTransfer opt = new ObjectParamTransfer();
12         opt.time = new Time();
13         opt.time.hour = 11;
14         opt.time.minute = 22;
15         opt.time.second = 33;
16 
17         System.out.println("time Attribute value of the ____________");
18         System.out.println("hour=" + opt.time.hour);
19         System.out.println("minute=" + opt.time.minute);
20         System.out.println("second=" + opt.time.second);
21 
22         opt.method(opt.time);//Pass object references as parameters to methods method
23         //Comparing changes after execution methods
24         System.out.println("After executing the method time Attribute value");
25         System.out.println("hour=" + opt.time.hour);
26         System.out.println("minute=" + opt.time.minute);
27         System.out.println("second=" + opt.time.second);
28 
29     }
30 
31     private void method(Time t) {
32         System.out.println("parameter t Attribute value of the ____________");
33         System.out.println("hour=" + t.hour);
34         System.out.println("minute=" + t.minute);
35         System.out.println("second=" + t.second);
36         System.out.println("Yes t and time Conduct==By comparison, the results are as follows:" + (t == this.time));
37 
38         System.out.println("change t Example variable values");
39         t.hour = 44;
40         t.minute = 55;
41         t.second = 60;
42     }
43 }
The results are as follows:
The attribute value of time: hour=11 minute=22 second=33 Attribute value of parameter t: hour=11 minute=22 second=33 Comparing t with time, the result is true: Change the instance variable value of t The property value of time after execution of the method: hour=44 minute=55 second=60

The object reference opt.time is passed to the method method method and then the attribute value of parameter t is output. It is found that the attribute value is the same as that of the original object reference. Further returning t==this.time results in true. After changing the instance variable value of t, the attribute value of the current reference opt.time also changes. This fully proves that parameter T and object reference opt.time point to the same object, that is, they point to the same memory space address. So the question arises, is the reference opt.time passing the memory address value to the parameter t or is it the parameter t itself? We don't know that. Here's another example to prove it.

 1 public class ObjectParamTransfer2 {
 2     Time time1;
 3     Time time2;
 4 
 5     public static void main(String[] args) {
 6         ObjectParamTransfer2 opt = new ObjectParamTransfer2();
 7         opt.time1 = new Time();
 8         opt.time2 = new Time();
 9         opt.time1.hour = 12;
10         opt.time2.hour = 23;
11         System.out.println("Value before exchange:");
12         System.out.println("time1.hour=" + opt.time1.hour);
13         System.out.println("time2.hour=" + opt.time2.hour);
14 
15         opt.swap(opt.time1, opt.time2);
16         System.out.println("Exchanged values:");
17         System.out.println("time1.hour=" + opt.time1.hour);
18         System.out.println("time2.hour=" + opt.time2.hour);
19     }
20 
21     private void swap(Time t1, Time t2) {
22         Time temp;
23         temp = t1;
24         t1 = t2;
25         t2 = temp;
26     }
27 
28 }
The results are as follows:
Value before exchange: time1.hour=12 time2.hour=23 Exchanged values: time1.hour=12 time2.hour=23

Here's a way to swap two references. But we found from the results that the output did not change after calling this method. It shows that this method does not exchange opt.time1 and opt.time2 references. In this method, only parameters T1 and T2 are exchanged, which verifies that the reference passed in the previous example is the address value of memory space, not the parameter is the reference (that is, not the reference passed). Operations on parameters do not affect the original reference, but only the contents of the same memory space as the reference refers to. This proves that reference type parameter passing is not passing a reference, but simply passing the reference's memory address value.

3. A Special Case in Parametric Transfer

In the example above, we already know that the basic type of parameter passes the value of the passed value, and the reference type parameter passes the value of the memory address. But I encountered a special situation in my learning process:

 1 public class Test {
 2     public static void main(String[] args) {
 3         String str = new String("abc");
 4         change1(str);
 5         System.out.println(str);
 6     }
 7     
 8     private static void change1(String str1) {
 9         str1 += "123";
10         System.out.println(str1);
11     }
12 }
Operation results:
abc123 abc

According to the conclusion above, the parameters of reference type pass the memory address value, str and str1 point to the same memory space, and the output str should change after str1 changes the contents of the memory space. But the content "abc" in the memory space that str points to has not changed. Here I understand that a String type reference passes content, not memory space address values. (Similar to the value passed in the first example) does not affect the content of the memory space referred to in the original reference. Maybe that's why String object content is immutable?

Because it is still at the basic stage, learning is not deep enough and I have some doubts. The above understanding may have deviations and errors, if there are errors, please point out.

Topics: Java Attribute