1. When the object is passed as a function parameter, it is passed as a reference to the original object, and changing the value of the parameter object affects the original object. 2. When the object is the return value of a function, passing it is also a reference pass, changing the passed object when it affects the original object 3. When object A assigns a value to another object B (regardless of whether B has been new to open space), B is a reference to the A object, and change B affects a 4. The only way to pass a reference is to copy the object by using the basic data type assignment, such as the last method in the previous example. The object that appears is a new object, not a reference to the original object supplement: 1. The string type in Java is also an object, but when the assignment is passed, the value is passed in the same way as the general data type. String is a special case. Summary: The transfer of objects is basically done by reference passing, and the only way to pass a non-reference is to use the basic data type assignment method (or implement a copy copy function yourself) after you have opened up a new object space.
Attach the test code and results:
String str = new String ("abc"); char[] ch = {' A ', ' B ', ' C '}; LOG.D ("Levin", str+ "\ n"); LOG.D ("Levin", String.valueof (CH)); Change (str,ch); System.out.print ("\ n"); LOG.D ("Levin", str+ "\ n"); LOG.D ("Levin", String.valueof (CH)); Person person = new person (); Person.name = "Bill"; Person.age = ten; Changeperson (person); LOG.D ("Levin", person.name+ "\ n"); LOG.D ("Levin", String.valueof (Person.age));
public void Changeperson (person person) { person.name = "Jobs"; Person.age =; }
06-07 12:33:57.178:d/levin (8091): ABC 06-07 12:33:57.578:d/levin (8091): ABC 06-07 12:33:58.548:d/levin (8091): ABC 06-07 12:33:58.988:d/levin (8091): GBC 06-07 12:34:00.158:d/levin (8091): Jobs 06-07 12:34:00.418:d/levin (8091): 50
Some summary of Java object reference passing and value passing