• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass by value or reference, question from K&B mock test

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first answer is A: 1,3,5,7,2. Because I think the update method does not do anything to nums and index in main method. Why does the nums[3] still become 14 after update?
pass by value.png
[Thumbnail for pass by value.png]
 
Stefan Evans
Bartender
Posts: 1807
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article explains it better than I can.

You are correct that the called method can't do anything to nums or index that will be reflected in the main method
Hence index in the main method remains at 2, even though the method increments its version of it.

But remember that array and object variables are effectively pointers/references to memory.
Java passes the reference/pointer of the array to the method by Value.
The method can't make any changes that affect the value of nums in main.
But it CAN make changes to what nums is 'pointing at', and those changes will be reflected.



 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another good article that's been a breakthrough for some folks.
http://www.javaranch.com/campfire/StoryPassBy.jsp

Note that an array is considered an object. Primitives are pass by value. Primitive wrapper objects and String act like primitives. The objects are also pass by value, but the value is a reference if you can get you head around that. So they say it is always pass by value. If you know C pointers, the value passed in the case of a passed java object appears to be a pointer. They use the term reference instead of pointer in java, but a lot of time they slip back into saying pointer probably because pointer is more descriptive and reference has another meaning as in reference/object.
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand the following code output is
1 14 5 7 9
1.2 2.2

It seems that initialize the variable as a new local variable change something...

 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello nemo,

Did you check the articles mentioned by Stefan and Guillermo? Is there any specific part you did not understand?

Yes, everything is passed by value in Java. But the catch is - everything (except primitives) is a reference itself.

Thus:
1) Both nums and numd are references to integer and double arrays respectively.
2) In method update(int[] nums) - we are playing around with actual contents of the array referred by nums. Please note that nums at line 14 is copied to nums at line 2. So - here, we've got 2 references to same array.
3) However, in method update(doublt[] numd) - we are forcing numd (at line 6) to refer to another array. Now, since this numd refers to different array, and since its scope is local to update(double[]) method, those contents are not reflected in actual array. Why? because this numd no more refers to original double[] array!

So - to summarize:
1) If you pass an object (i.e. reference to object) to method and play around with actual object - the change is reflected even outside method.
2) If you pass an object (i.e. reference to object) to method and assign some new object to that reference (or, force that reference to refer to new object) - the changes are local to that method only - and original object is not modified.

I hope this helps.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Anayonkar is right. The new creates a new object that is local to update(). The name is the same but it doesn't matter. One big thing to remember is that when you pass an object reference (pointer) to a function, inside that function you can never change its original value. There is no way to do that. You can use new to create a new object locally and assign it to that variable name or even set it to null but the original object that got passed is always object at the original address..

The line 2 changes the original array. The line 3 does not That's because you can't change what the original i points to. Look at the remote control analogy in my link, Line 2 is using your remote control and line 3 is smashing your remote control. All smashing it does is smash it - no effect on the controlled thing..

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This post has also a good explanation about what's going on. It's about local variables, but the concept is very similar.

I think now you are ready for one of my famous pop quiz questions What's the output of this code?
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:This post has also a good explanation about what's going on. It's about local variables, but the concept is very similar.

I think now you are ready for one of my famous pop quiz questions What's the output of this code?


arr would not change because String is immutable. If line10 becomes arr[0]=arr[0].toUpperCase();, the output would be JAVA rocks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:arr would not change because String is immutable. If line10 becomes arr[0]=arr[0].toUpperCase();, the output would be JAVA rocks!

Spot on!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic