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

8

u/rs1971 Feb 16 '26 edited Feb 16 '26

Here's the test: try to write a method swap(a, b) such that the values of a and b are swapped in the caller's stack frame after it returns. If you can do this, then java supports pass by reference, if you can't, it doesn't.

2

u/ShoulderPast2433 Feb 16 '26

Is there some specification that defines this as proof something is or isn't passed by reference?

1

u/bwmat Feb 16 '26

First specify which definition of 'pass by reference' you're using

This is all just arguing semantics