• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass-by-Reference?  RSS feed

 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So two of my friends and I were working on a few problems our teacher handed out, and one problem included completely randomizing the order of the numbers. We accomplished this, but not without some confusion from my friend and myself.


(I'll try to retrieve the actual code we used in a bit, I have the class with my 2 friends in a few hours; I tested this code and it doesn't work for some reason >.>)

Could someone explain to me how this sort of stuff actually works? While browsing the topics I found the same swap style used here as well: Tony Marchant's post
I think the part I'm not getting is how even though Java is "pass-by-value" - which I assume means only the values are passed through the parameters, leaving no 'bridge' of some sort back to the calling method unless a return type is established - the array local to the main method is still changed... outside of the method?

If not for my friend using such a nifty method, I would have probably just set up a global variable or set a return type for the method... is what I planned to do a bad thing, or just generally less efficient?

Just for a little extra attention, does anyone know where I might find a place where people's projects are explained thoroughly? I'd love to see a real-world example explained, because right now my AP Comp Sci teacher is forced to teach the very, very basics ("How do you declare an array?") due to some students only taking the course for the math credit (I've had to teach myself the majority of the material, as the class isn't even at Collections yet...)
This is really problematic because I want to take the AP test for this class... hard to tell if I'll pass if I don't even know this simple material...
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do realise that swap method call will not do anything? Nothing to do with pass‑by‑value, but all to do with arithmetic.
Go through this thread about random numbers, and I hope it will help explain the problem. But you have to read the whole thread.
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Cup Size -- a story about variables (both parts). It is a great explanation of java "pass-by-value". In short, all parameters are passed by value. But you can't store an object in a variable, you can store only a reference to that object. And if you can't store objects, you can't pass them. You pass only a reference to original object. You can modify that object using any reference to it. This all also applies to arrays because any array is an object.
 
Shashank Gollapudi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with pass by value, what is happening here is, you may not be passing the arguments the way you wanted to.

Here is how it is happening
On your line 15



you are always passing the second and third arguments as 0

Math.random() always returns a double value equal to 0.0 and less than 1.0.

here you are casting on Math.random() to an integer value and then multiplying with either 9 or 10.
TO be clear, you are calling ((int)Math.random())*9 = 0 * 9 (always)

So it is as simple as calling swap(array, 0, 0 )
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Math.random() part was just a syntax mistake, that has nothing to do with the question
What I was questioning was how the swap method manages to function without return anything or manipulating a global variable, and it is not only here that I'm showing you but also in someone else's post


Levi Neuxell wrote:While browsing the topics I found the same swap style used here as well: Tony Marchant's post [Save for parameters]



What's the concept behind it? It might be easier just to tell me what the process involving manipulating foreign variable without returning something is called (unless I'm missing something important here). But again, there may be something else going on inside his code, so I'll try to post it here in a few hours
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the swap method work? You're right, it doesn't modify any parameters and it doesn't return anything. It just modifies a mutable object. The calling code passes a reference to the List, or to the array, to the method. So now the method has a reference to the same mutable object. There's only one object, but two references to it. And it's a mutable object so everybody who has a reference to it will see the changes.
 
Aaron Shawlington
Ranch Hand
Posts: 50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this: http://blog.aaronshaw.net/2014/02/13/java-is-always-pass-by-value/ just the other day.
Possibly my car & car key analogy might make it clearer. Maybe not
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of a variable as holding an address, similar to a rolodex card. I have a card with my home address on it. In my house, my bedroom is blue.

Now, I can pass a copy of the card to a painter. I give him a COPY of my home address. He can then go to my house, and paint the room white. He doesn't have to tell me he is done, or tell me what color my bedroom is.

But the next time I go and look at my bedroom, it will be white.

These methods work the same way. The calling method (correlates to me) has the address (on a rolodex card) of the object (my house). it passes a copy of the address to the sub-method (the painter). The sub method then does its thing (paints the room). It then throws away its copy of the address. The calling method the looks at the object, and see the changes.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:He doesn't have to tell me he is done, or tell me what color my bedroom is.

Which would be just typical, especially when I wanted it blue. Back in my day...grumble...grumble...

Winston
 
Aaron Shawlington
Ranch Hand
Posts: 50
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Think of a variable as holding an address, similar to a rolodex card. I have a card with my home address on it. In my house, my bedroom is blue.

Now, I can pass a copy of the card to a painter. I give him a COPY of my home address. He can then go to my house, and paint the room white. He doesn't have to tell me he is done, or tell me what color my bedroom is.

But the next time I go and look at my bedroom, it will be white.

These methods work the same way. The calling method (correlates to me) has the address (on a rolodex card) of the object (my house). it passes a copy of the address to the sub-method (the painter). The sub method then does its thing (paints the room). It then throws away its copy of the address. The calling method the looks at the object, and see the changes.


And *if* java supported pass-by-reference the painter could change your actual address while you were at work. When you jump in the taxi home you might have a nice surprise to find that you now live at Buckingham palace. Or maybe a nasty surprise.
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is great, thanks for helping! I understand the concept, and thanks to Aaron I can kind of understand the difference between pass-by-value and -reference. Again, great help :3
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!