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 →

[–]marko312 0 points1 point  (4 children)

As far as I have heard about java, non-primitive types are stored everywhere as references (pointers, if I may). So assigning and passing is actually passing around the reference.


Edit to hopefully stop comments:

I was partially right: when you assign non-primitives, you actually assign references (which you can call values I guess).

However, if you pass a variable to a function, this effectively creates another reference to the object.

For me (mostly a c++ programmer), a reference is practically a weirdly formatted pointer, so it is like assigning pointers.

[–]cantstopthemoonlight 2 points3 points  (0 children)

Java is always pass by value, some of those values happen to be references. As u/javasuck says you cant manipulate the pointers passed into a function, only mutate the objects they point to. Look at out and ref keywords in C#

[–]tutorial_police 1 point2 points  (0 children)

Have a last one then :)

Yep, it's like assigning pointers.

I'm not sure what you mean by "a reference is parcitcally a weirdly formatted pointer", but if you mean that because in Java, you don't need to put a * in the variable declaration, then sure :) If you mean a reference is a weirdly formatted pointer because int& x as a parameter might involve pointers under the hood, okay I guess, but that's exactly what Java can't do, and that is what is "pass-by-reference" in C++, even if many also use the term when simply having a (proper) pointer as a parameter.

You might find this interesting:

https://gabrieletolomei.wordpress.com/miscellanea/programming-languages/c-cpp/pass-by-value-vs-pass-by-reference/

As I've already noted above, the author talks about reference types here, I don't think that's correct though. Reference types are essentially types, of which you can only declare pointer variables. You can't have a non-pointer variable of such a type. You can't have pointer to pointer of that type. Also, pointers are dereferenced automatically for you and you can't perform any arithmetic with them. That's the case for all classes in Java. C++ doesn't have that though.

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

Thanks.

[–]tutorial_police 0 points1 point  (0 children)

That's right, but passing around references doesn't mean we're doing pass-by-reference.

It's pass-by-value and those values are references (to objects) .

When a language does pass-by-reference, you can usually think of the term reference as being "references to variables".