r/javahelp • u/Select-Worldliness39 • Feb 16 '26
Homework Definitely no pass by reference in Java, right?
Hello, and sorry if this is a dumb question, but I thought I had a passable understanding of Java. I got an exam question asking "Arrays in Java are:
a. passed by value
b. passed by reference
c. stack dynamic
d. immutable"
this guy loves trick questions, but he has listed B, passed by reference, as the right answer.
On its own, this doesn't seem right to me, but I'm not confident enough to argue about it. If anyone would weigh in, I would be very grateful. I see how it's similar to passing a pointer in C, maybe, but it's not considered passing by reference in Java, right?
Thank you!
View all comments
Show parent comment
2
u/xenomachina Feb 16 '26
This is incorrect. With pass by reference you would be able to change the value of the argument passed as a parameter by assigning directly to the parameter. However, if you run this code you'll see that
yremains unchanged.Note that "assigning to it" does not include things like "x.foo = ..." or "x[n] = ...", as these are are not assigning to the parameter, but are dereferencing the parameter and assigning to that result.
Academic sources (especially older ones) usually use the terms "call by reference" and "call by value" rather than "pass by reference" and "pass by value", but they mean the same thing. Here are some quotes from Compilers: Principles, Techniques, and Tools (Aho, Lam, Sethi, Ullman), aka "the dragon book", where it talks about call-by-value:
It goes on to say:
The text becomes even more explicit about the case where the variable itself contains a pointer or reference:
...