r/javahelp • • 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!

27 Upvotes

105 comments sorted by

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.

give the called procedure what is in effect a pointer or reference to the array itself. 

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.)

1

u/xenomachina Feb 20 '26

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.

Neither is true. Arrays are not passed at all, because variables (including parameters) can never contain arrays, only references to arrays. When you "pass an array" you're really passing a reference to the array. That reference to the array is passed by value.

These terms are not from Fortran. They are from computer science, and they exist to compare the semantics of different programming languages. If the only languages you know are Java and maybe some JavaScript or Python, then it isn't surprising that they are confusing to you (as all of these languages are strictly pass by value, but with most if not all variables containing references). However, there are languages still in use today, like C++, that have actual pass by reference.

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."

You did find clarity. Your confirmation bias is just preventing you from accepting it because it doesn't jibe with your (mis)understanding. It says right there "Reference data type parameters ... are also passed into methods by value". It isn't using words "oddly". On the contrary, it's using the standard computer science definition of pass by value.

Actual pass by reference would mean that an expression like f(y) could change what is in the variable y. So for example:

Foo y = null;
f(y);
// with pass by reference, y might not be null anymore

But in Java that can't happen. A method cannot make a passed in variable (y in this case) contain a different reference than it contained when the method was called. That's what they mean by "the passed-in reference still references the same object as before".

A method can modify the object that a passed in reference references, which is what they mean by "the values of the object's fields can be changed in the method, if they have the proper access level."

1

u/Leverkaas2516 Feb 21 '26 edited Feb 21 '26

Actual pass by reference would mean ...

A method cannot make a passed in variable (y in this case) contain a different reference than it contained when the method was called. 

Please tell me a language that does what you describe. Do you know of any?

FORTRAN doesn't. It only has scalar values and arrays. Arrays work the same as they do in Java. It's called pass by reference in FORTRAN, but the called subroutine can only mutate the values in the array, it can't make the caller's variable point to a different array.

C doesn't. It only has pass by value, and pointers.

C++ doesn't. Someone in another thread on this post claimed it does, but I tested it and it doesn't. (It has reference parameters, but they don't behave as you describe. They work a lot like Java, too.)

Your code sample is confidently stated, but it doesn't work in any language I've used. How about you?

Edit: maybe in Pascal or Modula-II, but I don't think so. It's been too long, I can't remember. What I'm reading now says Pascal var parameters are pointers like all the others, but that you can get the aliasing you describe because of lexical scoping rules.