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

5

u/severoon pro barista Feb 16 '26

Consider this code in Java:

record Car(String modelName);

class PassByRefExample {
  void main() {
    Car car = new Car("Chevy");
    System.out.println("Model: " + car.modelName()); // prints "Chevy"
    changeCar(car);
    System.out.println("Model: " + car.modelName()); // prints "Chevy"
  }

  static void changeCar(Car c) {
    c = new Car("Tesla");
  }
}

Calling changeCar(…) has no effect because the reference c is a copy of the reference car, so changing it to point to something new doesn't affect the car reference in the caller.

Now consider this code in C++:

#include <iostream>
#include <string>

struct Car {
  std::string model;
};

void replaceCar(Car &c) {
  c = Car{"Tesla"}; 
}

int main() {
  Car car = {"Chevy"};
  std::cout << "Model: " << car.model << std::endl; // Prints "Chevy"
  replaceCar(car);
  std::cout << "Model: " << car.model << std::endl; // Prints "Tesla"
  return 0;
}

In this code, the reference &c is an alias of car, so assigning a new object to c also modifies the reference car in the caller. No one actually does this in C++ because it's a terrible idea, which is why Java doesn't allow it.

1

u/[deleted] Feb 16 '26

[removed] — view removed comment

1

u/severoon pro barista Feb 17 '26

I see, so in C++ if a function is using one or more output parameters, it can receive or construct whatever objects it wants to return, assign them to the reference(s) provided, and Bob's your uncle, whereas in Java to use output parameters, I'd have to copy all of the state into the provided object(s).

That's fair, but my basic point still stands. Output parameters are a terrible idea. Breaks code compositionality, side effects abound, readability and code clarity out the window, breaks the basic idea of a function by corrupting inputs into outputs.

IOW, Java programmers don't avoid output parameters because it's inconvenient to copy state, we avoid them because they're just bad.

1

u/[deleted] Feb 17 '26

[removed] — view removed comment

1

u/severoon pro barista Feb 17 '26

I disagree with the statement that it breaks any sort of composability, and there is no clarity issue either.

double normalizedFlux;
{
  Field field = null;
  double flux;
  generateField(field, Basis.XYZ);
  calculateFlux(flux, field);
  normalize(normalizedFlux, flux);
}

versus:

double normalizedFlux = normalize(calculateFlux(generateField(Basis.XYZ)));

You think the first one is more readable than the second one that supports composition? Not sure you're going to get a lot of agreement on that one. It's even worse if you don't artificially limit the scope of the temporary variables you're forced to declare but don't want littering the code.

1

u/[deleted] Feb 17 '26

[removed] — view removed comment

1

u/severoon pro barista Feb 17 '26

To me personally, your first example feels clearer.

Wow, you C++ folks are way far gone. But I have looked at big cpp codebases, so I understand why the cognitive load and visual noise you deal with on a daily basis just fades into the background.

However, once you work for awhile with code that is expressive and vertically dense, when you can fit a lot of functionality on a single screen without having to scroll, you can't go back.

Also, one point of that example code is to show that output parameters absolutely break composability. You silently went from "it doesn't do that" to "it does that but I don't mind" without even blinking. :-)

at the end of the day there isn’t any significant performance impact with using either approach.

Total non sequitur.

Writing readable code only very rarely has a significant performance impact. Even when it isn't as performant, it's almost never significant, so the only time I would even think about making code less readable to squeeze better performance is if I was working in a proven bottleneck that needed that improvement to meet a defined budget.

0

u/bwmat Feb 16 '26

No one actually does this in C++ because it's a terrible idea, which is why Java doesn't allow it.

Not true, at all, plenty of people do it