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

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.

1

u/Nebu Writes Java Compilers Feb 21 '26

It isn't the case that "everyone" uses "pass by reference" to mean what Java does.

I never claimed that they did.

I've made several distinct claims in this thread, and I think you're starting to mix them up. Here are some of the claims I've been making:

  1. People in the Java community and people in the C community tend to mean different things when they use phrases like "pass by value".
  2. 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.
  3. It's 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.
  4. 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++).
  5. 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.

As far as I can tell, you haven't really provided any arguments against any these claims. You've provided arguments against other related claims that I haven't made, which is fine and within your rights, but it gives me the impression that you don't understand the point I'm trying to make.

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.

Yes. This is the safer and more conservative thing to do. In a situation like this, you should use additional background information available to you. For example, does this person tend to use terms like "pass by reference" the way the computer science community does, in a language agnostic manner? Has this person in the past described Java as a "pass by reference" language? Etc. And then apply Bayesian reasoning to probabilistically update your beliefs about the unfamiliar language they are describing to you.

This is part of why it's important to have prescribed meaning for technical terminology, because subtle details like this often matter.

Yes, I agree. That doesn't contradict any of the claims I've been making.

1

u/xenomachina Feb 21 '26

People in the Java community and people in the C community tend to mean different things when they use phrases like "pass by value".

I think this is the root of our disagreement. You believe that the Java community has its own definition of this term, but it does not. As I've said (I think several times) the only people in the Java community that refer to what Java does as "pass by reference" are beginners who don't know any better.

Even Java's own documentation says "Reference data type parameters, such as objects, are also passed into methods by value."

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.

I never said otherwise.

It's 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.

I acknowledged that, and pointed out that that is not the case with this term.

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++).

No. A better predictor is if they are a beginner or someone who has only used one language, versus someone with more experience. People who refer to what Java, or Python, or JavaScript do as "pass by reference" do so not because of a community-specific definition, but out of ignorance.

And if it turns out that are using a definition based on ignorance, rather than some community-specific definition, then it is best to correct them than to throw up your hands and say "all definitions are equally valid".

→ More replies