• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass by value vs pass by reference  RSS feed

 
Suhaas Mohandos
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I use an array as in the example 1, the change is reflected in the calling method. But if I use example 2, the change is not reflected. Why?

Example 1:



Example 2
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here d is a local variable,now you have call this method as "display(d)" which is actually "display(new Dog("Max"))" as java is pass by  value and now the local variable d will also reference the object given as argument(whose reference name is also "d").you are getting all these confusion because you are using the same variable name for argument and  reference pass to the method.
see the statement------>int b=5; int a=b;
in the above declaration a and b are referencing the same int(datatype) but suppose if i increment the "a" do you think b is going to increment?"NO".same is your case you think if the local variable inside a method is changed then it will also change the another reference which is referencing the original object.only reason besides all the things(mentioned above) is just "java is pass by value".

Hope this will help!

kind regards,
Praveen.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your two examples are fundamentally different. In the first example you are changing a value contained in the array. In the second example you are changing the value of the local parameter variable itself.

If the first example had been 'value = new int[] {150}' or the second had been 'd.name = "Fifi"' (or, ideally, 'd.setName("Fifi")'), then you would see similar results for both.
 
Suhaas Mohandos
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Praveen

How come the change is reflected in the "calling method" in example1?


@Ole Sandum
If the first example had been 'value = new int[] {150}' or the second had been 'd.name = "Fifi"' (or, ideally, 'd.setName("Fifi")'), then you would see similar results for both.

1. So if I use new in the called method, the change will not be reflected in the calling method?

2. If you are saying primitives are pass by value and objects are also passed by value in java then give me a fresh example of both(primitive and object) where the change is not reflected in the calling method?

3. When  they say java is pass by value does it mean that changes made in the called method will not affect the calling method?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to keep in mind is that the reference is the value that is being passed.
Not the object that sits on the heap.

So you can use the reference to access the underlying object and call methods on it, but you cannot change the value of the reference and expect it to be reflected outside the method.
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suhaas Mohandos wrote:1. So if I use new in the called method, the change will not be reflected in the calling method?

That depends entirely on what you assign the value to. If you assign the new value to the parameter variable, no change will be reflected outside the method (because of pass-by-value). If you alter the object referred to by the parameter variable - for instance, you assign the value to an instance field of the referenced object - then you will see the change reflected outside the method. Not because of pass-by-reference (there is no such thing in Java). But because multiple variables can point to the same object.

Suhaas Mohandos wrote:2. If you are saying primitives are pass by value and objects are also passed by value in java then give me a fresh example of both(primitive and object) where the change is not reflected in the calling method?

Certainly:

Output:
----
myArray1: [2, 4, 8, 16, 32, 64]
myArray2: [1, 1, 2, 3, 5, 8]
myClass1: MyClass with name: John
myClass1: MyClass with name: Martha

myArray1: [2, 4, 8, 16, 32, 64]
myArray2: [1, 1, 2, 3, 5, 8]
myClass1: MyClass with name: John
myClass1: MyClass with name: Martha
----

As you can see, no change.

Suhaas Mohandos wrote:3. When  they say java is pass by value does it mean that changes made in the called method will not affect the calling method?

It means changes made to the parameter variable will not affect the variable that was passed to the method. That is, if you make the parameter variable point to another value, the passed variable will not point to the same value. Because, it isn't really the variable that is passed to the method, it is its value that is passed. Hence pass-by-value.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of people have difficulty with pass by value; here is a long discussion about how cats naturally implement pass by value.
You need to distinguish pass by reference, which can be implemented in C++ and some other languages, from passing a mutable reference type. If you have a mutable object, you can change its state, but not its value.The assignment in the method is not reflected in the original reference. If however, the original cat is fed, then you change the state from a hungry cat to a fed cat. Assigning elements of an array is similar; you are not changing the array but changing its state.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that arrays are objects in java. In the below example am I passing an object or value?

 
Junilu Lacar
Sheriff
Posts: 11479
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:We know that arrays are objects in java. In the below example am I passing an object or value?

What do you think is the answer?
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe we are passing a reference to the object.How is the caller method having modified value? Is it because we are actually modifying the object and not changing the reference?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raja,i am explaining with your mentioned code with a slight change(for simplicity)-name of method parameter in "change".See now
in the main method at line 6 you are calling by doing so the array reference "val" will get assigned to the method parameter named arrArg like below:

now in accordance with the method implementation of "change",it will cause to change the 0th element of array(mutable) referenced by arrArg.now you have to recall that java is "pass by value" so "arrArg" is referencing the same array which is the one referenced by "val".
raja singh kumar wrote:I believe we are passing a reference to the object.How is the caller method having modified value? Is it because we are actually modifying the object and not changing the reference?
.you are passing "reference(val,to array object") as argument in method "change".
.you are getting a modified array because the method arg is referencing the same array object,one in your main method,and yes this reference did not change in the implementation of "change".

kind regards,
Praveen.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java always makes a copy of the argument and passes the copy. The called method has a local copy of the data. If the method changes the data it changes the copy, so the original value is not changed.

When we pass a primitive like int this make perfect sense. The method gets its own int variable, a copy of the original.

When we pass an object we have to think very precisely. The value that is copied and sent along is a reference or pointer to the object. The method gets its own copy of the pointer, but it doesn't get its own copy of the object. If the method changes its copy of the pointer to point to a different object the original pointer is not affected. If the method changes some of the attributes of the object, it changes the original object.

In short: In Java, object references are passed by value and primitive types are passed by value.


https://coderanch.com/wiki/660293/Call-Reference-Call

I got the above paragraph from the above link. It is very clearly mentioned how exactly the value and objects are passed by value. I want to know the difference between objects passed by value in Java with pass by reference in C++. Is it not true that reference in Java is similiar to a pointer in C++?

Is allowed to post C++ code here in order to get a better understanding of the difference?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Assume I call the above using the following:

If Java were pass by reference then the above call to someMethod() would result in s referring to the SomeClass object created in someMethod.
Since it is pass by value then it doesn't.

It is possible to write code in C++ that acts like that.
In Java it isn't.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code how do I make the change made in m1 method to reflect in main method? I dont want to change the return type of m1 method
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:In the below code how do I make the change made in m1 method to reflect in main method?


A Boolean instance is immutable -- so, basically, you can't. Or at least, not in the context as discussed in this topic.

Henry
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I found one vague solution. Does anyone have a better one than this?

 
Junilu Lacar
Sheriff
Posts: 11479
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:In the below code how do I make the change made in m1 method to reflect in main method? I dont want to change the return type of m1 method

That's not a recommended practice, so why would you want to do that? Try to learn good ways to code instead of finding ways around built-in safeguards. you should prefer writing methods that don't have side effects. Side effects can lead to more surprises and less reliable code. Your code will also be more problematic in a multi-threaded environment if it has side effects.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I want to have a common boolean value which is shared by multiple classes and change made to the boolean variable in any of the classes should be visible to all the classes. How do I do that?
 
Junilu Lacar
Sheriff
Posts: 11479
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Well I found one vague solution. Does anyone have a better one than this?

Don't do that. That's just a whole lot of trouble for nothing. If you were on my team and insisted on doing this kind of thing, I would send you packing. If I were interviewing you and this is the kind of code you showed me, you wouldn't get hired. That should give you an idea how objectionable that kind of practice is to some people.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:But I want to have a common boolean value which is shared by multiple classes and change made to the boolean variable in any of the classes should be visible to all the classes. How do I do that?


Isn't this what instance/static variables are for?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Is it not true that reference in Java is similiar to a pointer in C++?

Yes, that is true, references in Java are similar to pointers in C++ in many ways. But that doesn't have a lot to do with pass-by-value vs. pass-by-reference.

Here is a C++ example of call-by-reference. In the main function, the function func is called, which modifies the value of the variable that was passed to it. So, it actually changes the value of the variable that was declared in the main function. Something like this is not possible in Java.
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't this what instance/static variables are for?

static variable is shared by objects of the same class. I want a solution where I can share a common boolean value across classes.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
static variable is shared by objects of the same class. I want a solution where I can share a common boolean value across classes.


Well, isn't that what the different access modifiers are for? Or what getters/setters are for?

I agree with Junilu. There are a lot of more common solutions / practices to this -- and certainly much more common than doing something like using modifiable wrapper classes so that local variables can be passed around via methods, to simulate C++ pointers.

Henry
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone post an example where multiple classes update the same shared variable?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:Can someone post an example where multiple classes update the same shared variable?


I think the main issue here is that you seemed to have hijacked another topic for your question. This topic is for the discussion of paramater passing -- and how changes are affected.

Henry
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, Sorry... yes, you are right..I understood that java is pass by value for both primitives as well as objects. I will continue in the other thread for a different topic or start a different thread if needed.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!