1

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);
    }
}
Ann
  • 347
  • 5
  • 14

8 Answers8

3

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.

enter image description here

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.

Brett Walker
  • 3,505
  • 1
  • 15
  • 36
  • In this line : User u2 = new User(); u2.name="dsafsa"; u = u2; , The user u in main will be "dsafsa" ? – Ann Jun 19 '15 at 11:09
  • `u` now point to entirely different user. Think `Person anotherReferencetoTheSamePersonObject` points to another `Person` way off the page. The original `Person person` is still pointing to the same thing. – Brett Walker Jun 19 '15 at 11:12
  • Thank you for your help . – Ann Jun 19 '15 at 11:17
1

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.

Dams
  • 917
  • 11
  • 23
0

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"?

Community
  • 1
  • 1
Piotr Rudnicki
  • 424
  • 2
  • 7
0

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

Sajith Eshan
  • 686
  • 4
  • 17
0

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.

Fran Montero
  • 1,581
  • 10
  • 21
0

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.

Vishwanath gowda k
  • 1,411
  • 21
  • 25
-1

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.

Paul
  • 13,100
  • 3
  • 17
  • 34
  • I don't understand the downvotes. Although your answer is not very clear, it is not wrong. I think the correct way to see it is that everything is passed by value, but when the arguments passed are objects, you are not passing the object itself, you are passing a *reference* to that object. And the reference is passed by value. – rpax Jun 19 '15 at 11:45
-2

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

Frowner
  • 644
  • 6
  • 19