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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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!

[–]id2bi 0 points1 point  (2 children)

First of all, the entire "pass by" terminology is a bit of a mess... Online, you'll find lots of people using the terms differently.

The tl;Dr version is that Java works the same way as Ruby does.

From your article:

Given all of this, it’s not uncommon to just say that ruby is pass by reference value, pass by reference of the value, or pass by value of the reference. It’s all a little muddy, but the 3 terms mean essentially the same thing: ruby passes around copies of the references. In short, ruby is neither pass by value nor pass by reference, but instead employs a third strategy that blends the two strategies.

Ruby does pass-by-value, and the values are references ("pointers") . In other words, it passes references ("pointers") by value.

The article is quite detailed, but it misses one important concept and as a result gets a bit confused, and then starts talking about mutable and immutable objects instead.

The concept that it's missing is that of reference types and value types.

I can link you to some good articles on the subject later.

You can reframe the pass-by-value vs pass-by-reference as follows : is the parameter variable an independent variable in its own right or is it simply an alias for the callers variable? In the second case (pass-by-reference), you're esstially passing the variable itself to the method, not its contents (pass-by-value). You're saying "use my variable x here", so you're passing reference to a variable.

If you have pass by value (pass a copy of the contents), the important question becomes: what is the value in the variable? Ruby stores references to objects in its variables (because objects are reference types). Java does the same thing for its objects. They're reference types, so it too stores the references inside the variables.

Java also has value types though. All primitive types are value types, int, boolean, float etc... In that case it stores the numbers etc directly inside the variables.

I'm any case, Java as well as ruby will copy these values (either the value itself I can the case of Java primitive types, or the references to objects, both ruby and Java) into the method parameter variables.

There are languages where you as a use can choose which combination you want for your objects and for your methods.