简体   繁体   中英

JAVA passing argument

I know that JAVA is passing arguments by value (copy). Why does this code returns John ?

public class User {
    String name;
    public static void main(String[] args) {
        User u = new User();
        u.name = "Sebastian";
        System.out.println(u.name);
        initialize(u);
        System.out.println(u.name);
    }
    public static void initialize(User u){
        u.name = "John";
        System.out.println(u.name);
        User u2 = new User();
        u2.name="dsafsa";
        u = u2;
        System.out.println(u.name);
        u.name = "Lilly";
        System.out.println(u.name);
    }
}

The reference to the object u is being passed to the initialize method is being passed by value (the value of the reference). This doesn't affect how u.name is accessed.

When you execute u.name = "John" then you are changing the u being passed into the method (via the reference). This is affecting the same u in the main method.

在此处输入图片说明

In this image Person person is like you User u in your main method. Inside initialize you User u is like Person anotherReferencetoTheSamePersonObject . So updating one updates the other.

It is scope problem, when you create a new user in initialize method

User u2 = new User();
u2.name="dsafsa";

and set it to the argument

u = u2;

You update only the reference of the argument in the scope of the method . But you dont change the user created in main method. Because Java pass arguments by value (the value of the reference).

If you dont change reference of argument

public static void initialize(User u) {
    u.name = "John";
    System.out.println(u.name);
    // User u2 = new User();
    // u2.name = "dsafsa";
    // u = u2;
    System.out.println(u.name);
    u.name = "Lilly";
    System.out.println(u.name);
}

You realy update the user created in main method. Because the value of the reference is always the one of the user creted in main method.

Because value in your case is a reference pointing to some object. The reference u in initialize method is a different one than u in main function, but they still point to the same object.

Top rated answer in this post explains it in the detail: Is Java "pass-by-reference" or "pass-by-value"?

Java always pass arguments by value.

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.[ 1 ]

In this you create a new User object and passes it's reference by value to the initialize method. Therefore, inside initialize method you refer to the same object you create in you main method. That's the reason why the original object gets modified.

So that u.name = "John"; modifies the object you created in main method. After u = u2; reference u points to the new object created inside initialize method. Therefore, u.name = "Lilly" modifies the second object.

1 - https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

In main you have u -(1)-> name , when initialize is called you have u' -(2)-> u -(1)-> name and when you do u = u2 then the reference (2) have changed u' -(2)-> u2 -(3)-> name2 . So in u.name = "Lilly"; you are changing u2's name and u in main is still unchanged.

Many answers above are suffiecient.. adding one more point

Java do not support pass by reference for primitive types, to make it possible Java has something called as Wrapper classes for primitive types through which you can contruct a object for primitive type and pass it by reference.

What are Wrapper classes?

As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.

You're wrong. Java passes arguments by value, if it's a primitive type. Arguments that are Objects are passed by reference. This means, if you manipulate an Object that was passed to a method as argument, you manipulate the same Object.

it is pretty simple and importent part.

when you write a class or using a class, they are not primitive type. privitive types are passed by value and the rest, not.

when you pass a variable that is not primitive it sent by its reference, which is like sending is place in memory.

any changes you make in it will change the original variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM