First look at the code and running results:
The comment is clearly written, so I won't say much about it.
I will talk about my conclusions. In fact, there is no concept of passing values or transferring addresses in java. java only has a reference concept. The reference is similar to the address transfer, but the address of the object is saved in a variable name, and the address points to an object. The reference is similar to passing a value. For the method, the form parameter and real parameter are both variable names, and the addresses in the variable names are the same. It means that the passed value is the value of the variable (the saved address) it is passed from the real parameter to the form parameter.
The above is my opinion. You are welcome to discuss this opinion together.
On the source code, do not want to write and want to try it again:
package com.elong.valueoraddress;public class Student {private int id;private String name;private int classId;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getClassId() {return classId;}public void setClassId(int classId) {this.classId = classId;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", classId=" + classId+ "]";}}
Package com. elong. valueoraddress; import org. junit. test; public class TestStudent {// The object address is stored in the variable name @ Testpublic void test () {Student student = new Student (); student. setId (1); student. setName ("elong"); student. setClassId (1); // the address of the object stored in student is changed (student); System. out. println (student);} public void change (Student student) {Student student2 = new Student (); student (2); student2.setName ("elongdeo"); student2.setClassId (2 ); // change the attributes of the objects pointed to by student. student. setId (3); student. setName ("dingyinlong"); student. setClassId (3); // student = student2 indicates assigning the address of the object pointed to by student2 to studentstudent = student2; // changing the attributes of the object pointed to by the original student2 student. setId (4); student. setName ("yinlong"); student. setClassId (4 );}}