java passed by value and pass by reference the most straightforward explanation!

Passed by value

Some white encounter this problem when writing java program:

public static void method( int as){
        as = 100;
    }
public static void main(String[] args){
		int as = 50;
		method(as);
		System.out.println(as);
   	}

The above code is output 50, as the value has not changed. As we passed in accordance with the idea as an argument to the function and change the value of ah? Why is there no effect?
answer:

  • We declare as a variable in the stack, after it is initialized to the space allocated on the heap
    Here Insert Picture Description
  • We will pass as execution method as a parameter method, equivalent method method as copied as a copy of our statement of
    Here Insert Picture Description
  • But we will copy as re-assignment method, changes in the heap will be pointed.
    Here Insert Picture Description
    This is the principle passed by value, and we are not operating as this parameter can be changed to point to our statement as a parameter passed just over the equivalent of a copy, will not affect the value as declared in the method, the output results still 50no change.

Passed by reference

We understand the value passed by, then passed by reference situation will not happen and as it passed by value?
Let's experiment a bit!
First, we'll get back to the initial value of a given age in the Student class, and write set, get method.

private int age = 20;
public int getAge() {
        return age;
    }
public void setAge(int age) {
        this.age = age;
    }

Accordance with the idea of ​​passing by value completion code:

public static void method_01(Student stu){
        stu.setAge(10);
    }
public static void main(String[] args){
		Student student = new Student();
        method_01(student);
        System.out.println(student.getAge());
	}

The final result is output 10, even modify it succeeds, it seems passed by reference and pass by value have differences. So in the end where the difference?
answer:

  1. Similarly, new new a new object, must exist in the stack, and student age of this object is an initial value, certainly in the heap memory allocated a space.
    Here Insert Picture Description
  2. The student as an argument method_01 this method, first be sure that the method parameter is a copy of the student.
    Here Insert Picture Description
  3. Method_01 execution method stu.setAge(10);, we can see from the chart stu point or age. Therefore, execution 在stu.setAge(10);time will change the value of age
    Here Insert Picture Description
    above is according to the basic principles passed by reference, it is passed by reference we will change the initial value of the object, initialized

to sum up:

  1. When you pass by value, we change the size of the value in the method does not affect the size of the original variable values. Pointing to the original variable method parameter points to is not the same, do not change the value of parameter points to have an impact on the value of the original variable.
  2. When passing by reference, process changes will change the value of the initial value of an object, because the object point and the point is the same method parameter, modify the attribute value of the parameter stack, the object will be affected.

Guess you like

Origin blog.csdn.net/fight252/article/details/90765977