In variousProgramThere are two methods in the design language: Value call and Reference call.
Java programming language is always usedValue call, that is, the method obtains a copy of all parameter values..
There are two types of method parameters:
- If the parameter is of the basic data type, a value of the basic data type is passed;
- If the parameter is an object,Copy object references. Note that it is not an object reference in C ++.. The referenced copy is the same as other copies, and an object is referenced at the same time.
A method cannot modify a parameter of the basic data type, but can modify the state of an object.
Be sure to note that in Java, the object is a copy of the reference, not a reference call. The inverse example is as follows:
Test procedure:
Package COM. xujin; public class test {public static void main (string [] ARGs) {employee [] Staff = new employee [2]; staff [0] = new employee ("Bob ", 1000); staff [1] = new employee ("Jim", 2000); // doesn' t workswap (staff [0], staff [1]); system. out. println (staff [0]. getname (); // bobsystem. out. println (staff [1]. getname (); // jimstaff [0]. raisesalary (0.5); // raise the salary by 50%. out. println (staff [0]. getsalary (); // 15001_system.out.println (staff [0]. GETID (); // 1system. out. println (staff [1]. GETID (); // 2} public static void swap (employee X, employee y) {employee temp = x; X = y; y = temp ;}} class employee {public employee (string name, double salary) {This. name = Name; this. salary = salary; id = nextid; nextid ++;} Public String getname () {return name;} public double getsalary () {return salary;} public int GETID () {return ID;} public void setname (string name) {This. name = Name;} public void setsalary (double salary) {This. salary = salary;} public void raisesalary (double percent) {This. salary * = (1 + percent);} private string name; private double salary; private int ID; Private Static int nextid = 1 ;}