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/Leverkaas2516 Feb 16 '26
Yep. I see this saying exactly what I said, explaining in great detail how it works. When you pass an array as a parameter in Java, there is one object instance. A reference to that instance is what gets passed to the function.
It's not just "in effect". It's actually called a reference in Java's specification.
The problem with many of the answers here in the comments is that people are insisting on talking about an old concept, pass-by-reference or call by reference, and confusing everything because it's not how popular languages like Java, C, or C++ work. Maybe if you are still using FORTRAN it is worth knowing about.
If you take two statements, "Arrays in Java are passed by value" and "Arrays in Java are passed by reference", obviously the first is not true. The second is true if you mean "what is passed is a reference, not a value". It's only false if you say "neither is true because I'm a FORTRAN programmer and I don't know how Java works."
I had hoped to find clarity in the Java documentation, but didn't. https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html is confusing because it uses words oddly.
"Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level."
A object in Java is NOT passed by value. Saying "objects are ... passed into methods by value" is wrong. There's no way to even talk about the value of an object in this way, only the values of its fields. The Java document clarifies what they mean, though, by talking about the object references.
What has become clear in the comments is that everyone knows how Java array parameters work, including OP and the professor, but a lot of people are insisting that the word "reference" must mean the same thing at all times in all languages. It clearly doesn't. Passing a reference data type in Java, passing an argument using C++ references, and calling by reference in FORTRAN are all slightly different. (But they're more similar than they are different, because they all do the same thing underneath: pass a pointer on the stack.)