From the beginning of my internship interview, I encountered this question in the interview: Is Java passed by value or by reference?

At that time, I could only recite the test questions on the back, but some of the answers on the Internet were still wrong, which led me to decide to write this article.

Although there are many articles on the Internet, BUT I still want to write my own article, if the writing is not good, please forgive me

But let’s do that before we get startedIs Java passed by value or by reference?This question! The answer, of course, is: Java is value passing

Given the conclusion, what is value passing and reference passing
  • Value passing: 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 in the function.

  • Reference passing: 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.

The following pictures are from Baidu Baike

The above code proves:

/** * prove that Java values are passed to demo **@author miao
 */
public class PassValueDemo {

    public static void main(String[] args) {

        int a = 1;
        String str = "1";
        User user = new User("Jen".6);

        System.out.println("Before calling the method a is:" + a);
        System.out.println("Before calling the method, STR is:" + str);
        System.out.println("User before calling method:" + user);

        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
        convert(a, str, user);
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");

        System.out.println("After calling the method a is:" + a);
        System.out.println("After calling the method, STR is:" + str);
        System.out.println("After calling the method, user is:" + user);
    }

    private static void convert(int a, String str, User user) {
        a = 2;
        str = "2";
        user.setAge(9);
        user.setUsername("Big strong");

        System.out.println("In the calling method a is:" + a);
        System.out.println("In the calling method STR is:" + str);
        System.out.println("User in the calling method is:"+ user); }}/ * * *@author miao
 */
class User {

    public String username;

    public int age;

    public User(String username, int age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername(a) {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString(a) {
        return "User{" +
                "username='" + username + '\' ' +
                ", age=" + age +
                '} '; }}Copy the code

The execution result of the program:

Before invoking a method, a is: 1 Before invoking a method, STR is: 1 Before invoking a method. User is: user {username='jen', the age = 6} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- in the method is called a: 2 call methods of STR for: 2 in the method is called the user: the user {username ='big strong', the age = 9} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a method is called after a is: after 1 call methods STR is: one method is called after the user is: the user {username ='big strong', age=9}
Copy the code

As you can see, the primitive int and final String have not changed, but the reference User has changed, so some people on the web have concluded that Java is value pass for primitive types and reference pass for reference types. But !!!! That’s the wrong conclusion, and if you don’t believe me, let me just add a line of code to the code above to see why this is the wrong conclusion, okay

In the convert() methodstr = "2";Let’s add a lineUser = new user ("小 mei ", 18)Let’s see what the result is.

The execution result of the program:

Before invoking a method, a is: 1 Before invoking a method, STR is: 1 Before invoking a method. User is: user {username='jen', the age = 6} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- in the method is called a: 2 call methods of STR for: 2 in the method is called the user: the user {username ='big strong', the age = 9} -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a method is called after a is: after 1 call methods STR is: one method is called after the user is: the user {username ='jen', age=6}
Copy the code
As can be seen from the above results, when we are inconvertMethod to addUser = new user ("小 mei ", 18)And then, all of a sudden, “pass by reference” is passed by variable value, right? Why is that?

This is because, in the Java language, there is essentially only value passing, which means that a Java parameter passes only a copy of it, not the parameter itself.

That is, the user passed in convert() is a copy of user user, pointing to the heap address of the same new user (” hua “, 6),

When user = new user (” Mei “, 18), the heap memory is changed, so any further operations are irrelevant to the previous heap address, confirming that a copy has been passed.

Failed.User = new user ("小 mei ", 18)before

performUser = new user ("小 mei ", 18)after

Conclusion: From this article, we can conclude that in the Java language only values are passed, and methods pass only copies of information, not the original content. We also learned that the underlying data types are generated directly on the stack, and that objects or arrays generate information on both the stack and the heap, and that references generated on the stack refer directly to data generated in the heap