Naman
Naman

Reputation: 2373

Pass by value/reference, what?

This program gives 6 as output, but when I uncomment the line 9, output is 5. Why? I think b.a shouldn't change, should remain 5 in main.

1  class C1{
2      int a=5;

3      public static void main(String args[]){
4          C1 b=new C1();
5          m1(b);
6          System.out.println(b.a);
7      }

8      static void m1(C1 c){
9          //c=new C1();
10         c.a=6;
11    }
12 }

Upvotes: 5

Views: 286

Answers (6)

user1357679
user1357679

Reputation:

In Java other than primitive types everything is pass by reference, though it says pass the copy of the reference.

Hence in your case in the first instance both of the pointers b and c points to the same object hence updated value is reflected back in the main method.

In second instance when you make c to point to a new object(by calling new) b is still pointing to the object created in main method hence updated value is not reflected in main method as b and c are pointing to two different objects.

Upvotes: 1

Luca Geretti
Luca Geretti

Reputation: 10286

Java is pass-by-value, in layman terms meaning that when you pass an argument you create a copy of the pointer variable. Object variables should really be read like pointers in C: this somehow makes things easier to understand, at least when coming from the C world (where C is pass-by-value only).

So when doing c=new C1(), or in general when performing any assignment to c, you are making c (which formerly pointed to the same location of memory that b pointed) point to another location. Hence the following c.a=6 is an assignment to a member of the object pointed by c, which is not the object pointed by b anymore.

Upvotes: 3

pcalcao
pcalcao

Reputation: 15990

You're passing to your method m1 a copy of a reference to an Object of type C1.

If you uncomment the line, you're taking that reference and pointing it somewhere else (not that the original reference is still pointing at the first object), so, when you print the value of b, you're printing the value of the original object, that you didn't change.

This question has some very good answers to it, and you should really give it a look.

Is Java "pass-by-reference" or "pass-by-value"?

Upvotes: 3

Bitmap
Bitmap

Reputation: 12538

Your method m1(b); is called in main, causing a new instance created in m1 to supercedes already created instance of C1 in main method. If you comment the instantiation in m1, the first instance in main then takes effect. This is because in java objects are passed by reference not by value.

Upvotes: 1

Yotes
Yotes

Reputation: 378

this is because c in m1(C1 c) is a reference to an object, and when you do c.a=6 now a is equals to 6 in this object. But if you do c=new c1() inside this method, c now is referencing a completly new object, then you are doing c.a=6 in this new object. Afterthat, you go out of the method, and the other c object still has the value 5

Upvotes: 1

mprabhat
mprabhat

Reputation: 20323

When you pass object in Java they are passed as reference meaning object referenced by b in main method and c as argument in method m1, they both point to the same object, hence when you change the value to 6 it gets reflected in main method.

Now when you try to do c = new C1(); then you made c to point to a different object but b is still pointing to the object which you created in your main method hence the updated value 6 is not visible in main method and you get 5.

Upvotes: 10

Related Questions