Java calls the function to change the value of the parameter when passing the reference.

—Restore content begins –

First, I wrote a small test, the code is as follows

 1 import java.util.ArrayList;
 2 
 3 public class QuoteTest {
 4     public static void main(String[] args){
 5         //test String
 6         String str = "value";
 7         TestClass tc = new TestClass();
 8         tc.ChangeVlaue(str);
 9         System.out.println("str now is"+str);
10         //test int
11         int num = 0;
12         tc.ChangeVlaue(num);
13         System.out.println("num now is"+num);
14         //test objectclass
15         TestClass testClass = new TestClass();
16         System.out.println("testClass now is"+testClass+";num:"+testClass.num);
17         tc.ChangeVlaue(testClass);
18         System.out.println("testClass now is"+testClass+";num:"+testClass.num);
19         //test Arrry
20         ArrayList<String> liststr = new ArrayList<String>();
21         liststr.add("str1");
22         tc.ChangeVlaue(liststr);
23         System.out.println("liststr now is"+liststr);
24     }
25 }
26 
27 class TestClass{
28     public TestClass(){
29         System.out.println("TestClass constructor ");
30     }
31     int num = 0;
32     public void ChangeVlaue(String str){
33         str = "changed value";
34     }
35     public void ChangeVlaue(int num){
36         num ++;
37     }
38     public void ChangeVlaue(TestClass testClass){
39         testClass.num ++;
40         testClass = new TestClass();
41         testClass.num ++;
42         //hangeVlaue(testClass.num);
43     }
44     public void ChangeVlaue(ArrayList<String> liststr) {
45         liststr.add("str2");
46     }
47 }

The output is as follows:

TestClass constructor
str now isvalue
num now is0
TestClass constructor
testClass now isTestClass@2503dbd3;num:0
TestClass constructor
testClass now isTestClass@2503dbd3;num:1
liststr now is[str1, str2]

When a function is called to pass in values, the internal value attributes of the parameter, such as attributes and values in the collection, are modified, but the address of the parameter does not change, and some simple types are not modified.

 

There is a doubt in the test:

What’s the principle of a new object? When you first modify the num of 39, the num++ will be displayed when the 18 row is invoked, but if it is placed behind 40, it will not be +.+, but are the two addresses not the same?

new One object:

1.javacCompile.Java source files to form.Class bytecode files.
2.new SubClass()Object, first check whether there is a parent class, there is a parent class, class loader (ClassLoader) will first read the parent class file into memory, create a java. lang. Class object, then load the subclass, the class loader will read the subclass class class file into memory,Create a java.lang.Class object;
3.First initialize the static properties of the parent class and initialize the static block of the parent class.
4.Then initialize the static properties of the subclass and initialize the static code of the subclass.
5.Allocate memory space in the heap memory, allocate memory addresses, at this time because of the unique properties of the parent class in the heap memory for the parent class object allocation space.  
6.Initializes the unique properties of the parent class.  
7.Initializes the structure block of the parent class.  
8.Initializes the corresponding construction method of the parent class object.  
9.Allocate memory space in the heap memory, allocate memory addresses, at this time because of the unique attributes of subclasses in the heap memory for subclass objects allocated space.  
10.Initialize the specific properties of the subclass.  
11.Initialize the code block of the subclass.  
12.Initialize the corresponding construction method of the subclass.  
13.Assign the memory address of the subclass to the reference object in the stack.  

 

Leave a Reply

Your email address will not be published. Required fields are marked *