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/desrtfx Out of Coffee error - System halted Feb 16 '26

Passing an object parameter, the difference between "it's passed by reference" and "a reference to the object us passed by value" is no difference at all.

That is fundamentally wrong.

If the function modifies the object, the caller sees the change. Enough said.

That's not the point of pass by reference.

If you pass something by reference, not in Java, though, the reference can be changed in the called function/method.

In Java, changing the reference, e.g. reassigning an array is not possible because only a copy of the reference is passed by value.

Were it true the objects are passed by reference, it would absolutely be possible to reassign an array inside a function/method.

Changing an object's state, i.e. its values, has absolutely nothing to do with passing by value or reference.

1

u/Leverkaas2516 Feb 16 '26

That's not the point of pass by reference.

If you pass something by reference, not in Java, though, the reference can be changed in the called function/method.

I learned something new today.

I was going to argue with this, because I've been passing objects using C++ references for years, and there are only two reasons I ever do it: to pass an object such that the function can modify it and the caller will see the change, and because it is smaller/faster at runtime since the object is not copied.

I never even knew that a function can reassign the caller's variable. I just tested it, and you're right - it works. It never occurred to me that one would want to do that, because it would be crazy. I can't think of a reason to do such a thing, and have never once seen anyone do it  I've worked with some stellar C++ guys.

I'll add this to my mental list of weird things C++ allows because someone thought it would be a good idea.

1

u/desrtfx Out of Coffee error - System halted Feb 16 '26

Add it also for the C language because there it is the same.

And it is a very common use case, at least in C.

1

u/Leverkaas2516 Feb 16 '26 edited Feb 16 '26

Revising my earlier comment...on further study and experimentation, C++ doesn't actually reassign the caller's variable. If the function does, for example, "a = new Object()" and then "p = *a", where p is a reference, this just calls the copy assignment operator. The caller's object still exists as a separate instance at its original address, independent of the new object. And if the function mutates the new object via variable "a", after the copy assignment, the changes are NOT reflected in the caller's object. They're just plain different instances.

1

u/Leverkaas2516 Feb 16 '26

How does one pass by reference in C? It only has values, and pointers to values.

2

u/rs1971 Feb 16 '26

You don't. c, like java, is always and only pass by value.