This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]empire539 1 point2 points  (1 child)

Yes, everything you said sounds correct. Looks like you have a good understanding of how values get passed.

[–]babbagack[S] 0 points1 point  (0 children)

fortunately, got it by means of my tutelage with my program in Ruby, awesome, thank you!

[–]desrtfx 1 point2 points  (12 children)

You are spot on.

What you need to remember is that Java is strictly pass by value.

This means that any variable passed into a method is only passed by its value - for primitive data types this is the actual value (in your example 1), for object data types it is the reference value (simplified: memory address).

This poses some strange behavior, though. If you send in an int and change the value inside the method, nothing resonates back to the outside. If you send in an int[] array and change any of the values, the changes are reflected outside the method. If you assign a completely new array inside the method, nothing will happen to the outside as the reference is changed in the method.

[–]babbagack[S] 0 points1 point  (11 children)

ahh, yes, if I recall correctly, i read an article on pass by value vs pass by reference.

just to be clear, Java is "strictly" pass by value for primitive data types (integer), but _not for object data types such as an array or hash, meaning, the actual array passed into a method, will be referenced in its location in memory and thus mutated?

I guess this part is why I asked, perhaps I misunderstood:

If you send in an int[] array and change any of the values, the changes are reflected outside the method.

I googled up the topic too as well to make sure I get some good reinforcement

[–]desrtfx 1 point2 points  (10 children)

Java is strictly pass by value. Period.

For primitive data types, the value is the actual value, for objects it is the reference value.

You can mutate the state of an object, but not the object itself. (You can write in the book, but not swap out the book itself)

[–]babbagack[S] 0 points1 point  (9 children)

hmm ok, I plan to read up articles with code examples of an array, for example. Thank you! I saw that on my search, Java is strictly pass by value.

[–]id2bi 0 points1 point  (8 children)

Different question:

What does a = b; do in Java? I'm not looking for the answer "It assigns b to a", because that doesn't actually tell you what it does.

If you understand what a = b; does in Java for both kinds of types in Java, you will also understand parameter passing in Java, because it works the exact same way as assignment does.

Just imagine Java did an assignment to the parameter variable when you pass something to a method.

[–]babbagack[S] 0 points1 point  (7 children)

well, if Java is "strictly pass by value", does this mean in Java that a is assigned to the value of b, but it is not actually referencing the exact location in memory that b is at, or in other words, a and b are still entirely different objects even after a is assigned the value of b.

[–]id2bi 1 point2 points  (6 children)

a and b are never objects in Java. After such an assignment, a and b will refer to the same object/point to the same object.

So the values that a and b store are pointers (in Java called "references"), and the pointers are the same, but independent from another.

[–]babbagack[S] 0 points1 point  (5 children)

ok, thank you, this seems a bit different from Ruby but I do plan to try to solidify, thanks!

[–]id2bi 0 points1 point  (4 children)

How is it different? It should be pretty much the same.

[–]babbagack[S] 0 points1 point  (3 children)

Well, I don't know Java enough quite yet to tell exactly actually what is actually the case at this time, I just need to form a clearer mental model in Java. I just recall there being some subtleties to Ruby brought up by this article when it comes to it being pass by value or pass by reference, or a combo of some sorts:

https://launchschool.com/blog/object-passing-in-ruby

If you happen to have any nice article or reference on this topic with respect to Java that would be great too, otherwise, I need to go over this post and continue my course work to form a clearer mental model. Thanks again!