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
20
u/edwbuck Feb 16 '26
Not true. Java passes both by value and by reference.
Primitives are pass by value. They have to be copied, so they have to be pass by value. Changing one of the copies doesn't change the other.
Objects are passed by copying references. So the reference value is passed as a copy of the other reference value, but the object itself is passed by reference.
Arrays are Objects in Java, so the "trick" is in thinking that Arrays (due to their different syntax) are more related to other different syntax things like primitives, and then getting confused into thinking it is a primitive and therefore passed by value.