Are parameters passed by reference or by value in Java?

This question often comes from a C++ background (likely the interviewer’s), as it uses terminology used in C++. To begin, let's clarify this terminology.

In C++, a reference is a variable that acts as an alias for another variable. Changing the reference also changes the original variable. Java does not have this feature, which becomes clear when thinking about something like a reference to an int.

In Java, a reference is more like a pointer to an object's memory address, a concept similar to pointers in C++.

Passing a parameter by value means copying the value into the method’s parameter variable. Passing by reference means passing the reference, essentially using the same variable.

In Java, a method parameter is always a copy. This means parameters are always passed by value, but that value might be a reference to an object. The code example below demonstrates this.
Are parameters passed by reference or by value in Java?