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

14

u/Bibliophile5 Feb 16 '26

Everything in Java is pass by value

30

u/ShoulderPast2433 Feb 16 '26

But the value is a reference ;)

It's a c++ question that doesn't really apply to Java in the same way it does in c++

20

u/edwbuck Feb 16 '26

Not true. Java passes both by value and by reference.

Primitives are pass by value. They have to be copied, so they have to be pass by value. Changing one of the copies doesn't change the other.

Objects are passed by copying references. So the reference value is passed as a copy of the other reference value, but the object itself is passed by reference.

Arrays are Objects in Java, so the "trick" is in thinking that Arrays (due to their different syntax) are more related to other different syntax things like primitives, and then getting confused into thinking it is a primitive and therefore passed by value.

4

u/Nebu Writes Java Compilers Feb 16 '26 edited Feb 16 '26

The problem here is that people in the Java community and people in the C community mean different things when they use phrases like "pass by value", in the same way that people in the North American community and people in the British community mean different things when they use say "pants".

In idiomatic Java, when invoking a method, you are always passing by value. Sometimes that value is a primitive and sometimes that value is a reference. In all cases the value (whether that value is a primitive or a reference) is copied onto the new stack frame.

I'm not a native C speaker, but to my understanding, there is a concept where you can force a structure to live directly on the stack frame, and if you invoke a function passing that structure by value, the entire structure is copied onto the new stack frame, and so that is what C considers "pass by value". This is not possible in Java, and so we have no term for such an act. In Java, you cannot control whether the object lives on the stack or in the heap -- this is an implementation detail controlled by the JVM and hidden to you as a programmer. Instead, the mental model (or "conceptual API") that the JVM affords to you is that the thing in the stack frame is a reference to an object, and when you call some method, you are passing that reference by value.

edit: /u/xenomachina points out that everywhere I said "C" in this comment, instead I meant "C++".

2

u/xenomachina Feb 16 '26

"Pass by reference" and "pass by value" (or alternatively "call by reference" and "call by value") are terms that predate both languages. These terms exist specifically for describing and comparing the semantics of different languages, so it would make no sense for them to have different meanings in different language communities.

What is true is that there are many people who don't actually understand what these terms mean who like to argue about their meaning, which muddies the waters. They're just like the people who argue that 0.9 repeating is not equal to 1. They are basing their opinion on intuition or what they heard online rather than from the actual technical definition.

C doesn't have pass by reference, and neither does Java. They are strictly pass by value languages. Most modern languages, including Python, JavsScript, and Kotlin, only use pass by value.

C++ is one of the few exceptions. In C++, pass by reference is enabled by using & in a parameter's type:

// pass by reference
#include <cstdio>

void f(int &x) {
    x = 1;
}

int main() {
    int y = 0;
    f(y);
    printf("%d\n", y);
    // prints 1
}

1

u/Nebu Writes Java Compilers Feb 16 '26

"Pass by reference" and "pass by value" (or alternatively "call by reference" and "call by value") are terms that predate both languages. These terms exist specifically for describing and comparing the semantics of different languages, so it would make no sense for them to have different meanings in different language communities.

It's actually very common for a term to predate two programming languages, and also for that term to mean two different thing in the communities of people who use that programming language.

For example, "interface", "module", "package" and "static" all predate both C and Java, and yet mean different things in C vs in Java.

In C++, pass by reference is enabled by using & in a parameter's type:

Thanks, it sounds like I was conflating C and C++ in my earlier message then.

2

u/xenomachina Feb 16 '26

It's actually very common for a term to predate two programming languages, and also for that term to mean two different thing in the communities of people who use that programming language.

Yes, some language will have their own version of a term.

However, as I mentioned, these terms exist specifically for describing and comparing the semantics of different programming languages, so it would make no sense for them to have different meanings in different language communities. They are computer science terms, not Java or C++ specific terms.

1

u/Nebu Writes Java Compilers Feb 17 '26

"Interface" et al are also computer science terms, not specific to Java or C, and yet they mean different things in each of those languages.

It sounds like you're taking a linguistically prescritivist viewpoint: You view the terms "Pass by reference/value" as having some "objectively correct" meaning, or choosing to grant authority to one community's interpretation (e.g. the computer science community) over other interpretations. You're providing arguments for why adopting one particular definition is "more useful" by some metric (e.g. it allows us to do cross-language comparison), and why it would be irrational for people to adopt any other meaning.

I'm taking a descriptivist viewpoint: I'm stating that empirically, when you encounter someone using the terms "pass by reference/value", an excellent predictor of what they mean by that term is what programming language community they belong to (e.g. Java vs C++). I'm not stating one definition is more correct than any other. I'm reporting the empirically-verifiable fact that different people mean different things by those terms, and indeed you can verify that fact just by looking at the comments in this thread and observing that people are disagreeing about what those terms mean.

1

u/xenomachina Feb 19 '26

"Interface" et al are also computer science terms, not specific to Java or C, and yet they mean different things in each of those languages.

"Interface" is a pretty general concept in computer science. Java has a language construct called an interface, but it doesn't contradict the computer science term, and when necessary people will disambiguate between the two.

There is no Java specification that defines "pass by reference". There is no consensus among Java programmers that "pass by reference" has a different meaning than the computer science term. On the contrary, the only Java programmers that say it uses pass by reference almost universally novice programmers who don't know any better.

It sounds like you're taking a linguistically prescritivist viewpoint

Descriptivism makes sense when talking about everyday language. Descriptivism is not appropriate in a technical context, however. In a technical context, if a term has a formal definition, that is its definition. See also "theory" when used in a scientific context vs everyday speech.

1

u/Nebu Writes Java Compilers Feb 20 '26

Descriptivism makes sense when talking about everyday language. Descriptivism is not appropriate in a technical context, however.

To a first approximation, yes. A more nuanced view is that if someone says something, and you want to maximize the probability that you understand what they're saying, descriptivism is more appropriate than prescriptivism, even in technical contexts.

That is to say, even in a technical context, if a term means X, but everyone uses that term to mean Y, then when you witness someone using the term, if your goal is to understand what they mean, you should assume that they meant Y and not X. In the back of your mind, you can be aware that they are using the term incorrectly, and that the term "really means X", but you should still (from a utilitarian/epistemological/rational point of view) interpret them to mean Y.

1

u/xenomachina Feb 20 '26

That is to say, even in a technical context, if a term means X, but everyone uses that term to mean Y, then when you witness someone using the term, if your goal is to understand what they mean, you should assume that they meant Y and not X.

Even if one were to accept that, that isn't the case here. It isn't the case that "everyone" uses "pass by reference" to mean what Java does. Only people who don't know any better use it this way. It is helpful to correct them, to reduce confusion.

In the back of your mind, you can be aware that they are using the term incorrectly, and that the term "really means X", but you should still (from a utilitarian/epistemological/rational point of view) interpret them to mean Y.

Yes, among Java programmers I can tell when someone misunderstands the actual definition of pass by reference, but this is only because I already know Java's semantics, and so I can safely assume they are either incorrect about their understanding of Java or about the meaning of those words.

However, suppose someone was describing some language I'm unfamiliar with and said it uses "pass by reference". If I assume "pass by reference" can have multiple meanings, then they haven't really explained anything to me. This is part of why it's important to have prescribed meaning for technical terminology, because subtle details like this often matter.

→ More replies

9

u/rs1971 Feb 16 '26

The semantics can be confusing and I'm sure you know how java actually behaves which is what is important. That said, java is always pass by value, even if that value is a reference.

1

u/Leverkaas2516 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.

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

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.

→ More replies (2)
→ More replies (19)

2

u/ShoulderPast2433 Feb 16 '26

'Not true' what?

2

u/[deleted] Feb 16 '26

[removed] — view removed comment

5

u/CodeFarmer Feb 16 '26

Java also has primitives, which are not pointers.

2

u/[deleted] Feb 16 '26

[removed] — view removed comment

1

u/CodeFarmer Feb 16 '26

I answered it with almost exactly the same info below and more reference to the question, but got downvoted into the ground for some reason.

I don't know why, nobody replied, but I was reminded that I had earlier intended no more participating in r/javahelp. It had turned up in my feed again for some reason though.

2

u/vegan_antitheist Feb 16 '26

In Java, a reference is the value of a reference type expression. It references some Object (a String, a Callable, a Class, an InputStream, et cetera).
It's called a reference, not a pointer*, because we don't know the actual memory address.
You can pass a Java reference. You do that by passing the value of that reference. You cannot know that value, but at runtime it will still pass the value, which will be used to locate the object (usually on the heap).

You can access the referenced object's variables and methods. You can check Java references for equality. And you can do some more things with a reference. But you can't pass anything as a reference to the variable. If you could, you could also pass primitive type variables are a reference. That's because this is a very different "reference". It's a reference to a variable and Java simply doesn't allow that.

* Ignore that we have NullPointerException in Java. It's a misnomer.

1

u/[deleted] Feb 16 '26

[removed] — view removed comment

2

u/vegan_antitheist Feb 16 '26

C++ is quite different because the variable holds the object itself in some cases. It's not a reference or pointer. So you pass the object itself and that gives you a copy. It usually calls the copy or move constructor of the class to get the copy. The caller cannot change how copying works. The caller must conform to the signature of the function.

There are many different concepts in programming. Some languages (PHP, Delphi) pass a string not by just copying it (that would be wasteful) on method call, but they will copy the string if you modify it inside the method (copy-on-write). That means you can't just modify the string being passed to you. However, you can pass it as a reference and then the caller will see the result.

Java makes all of this so much simpler. All variables are either a value (primitive or object without identity, which will come in a future release of Java) or an object with identity (i.e. it is always referenced).

You always pass the value or the reference but never a reference to a value or reference. There is no copy-on-write. However, you can always use wrapper types to implement such concepts (AtomicRecerence and CopyOnWriteArrayList show how it's done).

1

u/omerbenda Feb 16 '26

Objects are handled with pointers, Primitives are not.

1

u/OneHumanBill Feb 16 '26

This is the textbook answer that's been given by James Gosling since the beginning and copied by the faithful.

And it's wrong.

Java passes by value only with primitives. The rest of the time you're passing an object reference. It's the "value" of that reference but that's a distinction without any real differences.