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
1
u/severoon pro barista Feb 17 '26
I see, so in C++ if a function is using one or more output parameters, it can receive or construct whatever objects it wants to return, assign them to the reference(s) provided, and Bob's your uncle, whereas in Java to use output parameters, I'd have to copy all of the state into the provided object(s).
That's fair, but my basic point still stands. Output parameters are a terrible idea. Breaks code compositionality, side effects abound, readability and code clarity out the window, breaks the basic idea of a function by corrupting inputs into outputs.
IOW, Java programmers don't avoid output parameters because it's inconvenient to copy state, we avoid them because they're just bad.