Objects operated in Java are all references to operation objects. Objects themselves are stored in the heap, while objects'references are stored in registers or stacks. The Java language passes parameters by value and objects by reference; (It should copy values if they are of the original type, and references if they are of the object, not the real object itself. If other references are assigned to him in the method, the only change is the reference, not the object itself. So the value of the object remains unchanged. If the method changes its value, it is the object itself that changes. In fact, the value of this incoming function is a copy of the object reference, that is, the address value of the reference is passed. In addition, the String class is final ized, immutable, it will open up a new space in memory, so the last example can not change the value of string.
Here are two examples:
1. Define a type Value first
- public static class Value {
- private String value = "value";
- public String getValue() { return value; }
- public void setValue(String value) { this.value = value; }
- }
2. Write two functions newValue and modifyValue: newValue will point the input to a new object, and modifyValue will call the setValue method of the input to modify the value of the object.
- public static void newValue(Value value) {
- value = new Value();
- value.setValue("new value");
- System.out.println("In newValue, HashCode = " + value.hashCode() + ", value = " + value.getValue());
- }
- public static void modifyValue(Value value) {
- value.setValue("new value");
- System.out.println("In modifyValue, HashCode = " + value.hashCode() + ", value = " + value.getValue());
- }
3. Simple test code
- public static void main(String[] args) {
- Value value1 = new Value();
- System.out.println("Before modify, HashCode = " + value1.hashCode() + ", value = " + value1.getValue());
- //Pointing value1 to a new Value object
- newValue(value1);
- System.out.println("After modify, HashCode = " + value1.hashCode() + ", value = " + value1.getValue() + "\n");
- Value value2 = new Value();
- System.out.println("Before modify, HashCode = " + value2.hashCode() + ", value = " + value2.getValue());
- //Modify the internal value of the object using object's set method
- modifyValue(value2);
- System.out.println("After modify, HashCode = " + value2.hashCode() + ", value = " + value2.getValue());
- }
4. Execution result log:
- Before modify, HashCode = 12677476, value = value
- In newValue, HashCode = 33263331, value = new value
- After modify, HashCode = 12677476, value = value
- Before modify, HashCode = 6413875, value = value
- In modifyValue, HashCode = 6413875, value = new value
- After modify, HashCode = 6413875, value = new value
Example 2:
Example 3:
public class Test3 { public static void change(int []a){ a[0]=50; } public static void main(String[] args) { int []a={10,20}; System.out.println(a[0]); change(a); System.out.println(a[0]); } }
Obviously the output is 10.50. What is actually passed is the reference address value.
Memory analysis:
Example 4:
class Emp { public int age; } public class Test { public static void change(Emp emp) { emp.age = 50; emp = new Emp();// Create another object emp.age=100; } public static void main(String[] args) { Emp emp = new Emp(); emp.age = 100; System.out.println(emp.age); change(emp); System.out.println(emp.age); System.out.println(emp.age); } }
The output is: 100 50 50.
Memory analysis:
For String classes:
public class Test { public static void change(String s){ s="zhangsan"; } public static void main(String[] args) { String s=new String("lisi"); System.out.println(s); change(s); System.out.println(s); } }
The output is: Lisi lisi, because the String class is final modified and immutable, it will open up a new space in memory.