Is Java passed by value or by reference

Cause of misery

Why is it tragic? Because despite the number of articles I’ve read about Java value passing or reference passing, I’m still getting crushed by the interviewer during the interview. It’s tragic.

So I made up my mind to write this blog…

The crux of the matter

I think in order to figure out whether Java is value passing or reference passing, you first have to figure out what value passing and reference passing are. The concept introduction of Baidu Encyclopedia is quoted

  • Value passed

Value passing is when a function is called and a copy of the actual parameter is passed to the function so that the actual parameter is not affected if it is modified.

  • reference

Reference passing refers to passing the address of the actual parameter to the function when the function is called. Then the modification of the parameter in the function will affect the actual parameter.

From the description of Baidu Encyclopedia, we can see that a big difference between value passing and reference passing is whether the operation in the function will affect the actual parameters after passing in the function. For Java, values that can be passed into a method can be divided into two types, one is a basic data type, such as int,double,long, etc., and the other is a reference type, such as String, or a custom class. So there are two ways to distinguish between value passing and reference passing in Java.

How basic data types are passed

Let’s first look at an example of how basic data types are passed

public class TestRunTime {

    public static void main(String[] args) {
        int i = 3;
        TestRunTime test = new TestRunTime();
        test.exchange(i);
        System.out.println(i);
    }

    public void exchange(int x){
        x = 5; }}Copy the code

For this example, the final output is 3, and the change in the method does not affect the original value. We should think about this in terms of the way values are stored in primitive data types, where values are stored directly in variables and for such a local variable, in local variables. But the base data type also depends on the location, when you data the local variable, the operation in the method does not affect the variable, the value is just a copy of the value.

How reference types are passed

The first thing we need to know about the passing of reference types is that the object we create will be stored in the heap, and the stack will hold the in-heap address, as follows. The way we create an object is we actually create an object on the stack to hold the address of the object in the heap, and we create an object in the heap to hold the actual content.

When we put the object passed as a parameter to the method, the process is to address a copy of an object, the arguments in the parameter points to the heap that address, by this time the problem comes, if we are to object changes in the way, we will find that the method of argument content changed, will think that this is a reference, but in fact is wrong, this is the concept of confusion, Let’s look at the definition again. The difference between passing a reference and passing a value is whether we change the address of an argument that we pass in to a method. We don’t affect the argument, and when we change the reference of a parameter, the reference of the argument doesn’t change either. As follows:

public static void main(String[] args) {
        Test test = new Test();
        User user = new User();
        System.out.println("Before operation"+user);
        test.work(user);
        System.out.println("After operation"+user);
    }


    public void work(User user){
        user = new User();
    }
Copy the code

As shown in the result, we can see that the address modification of the parameter in the method does not affect the argument, so Java passes by value rather than by reference.