The terms "pass-by-value" and "pass-by-reference" have special, precisely defined meanings in computer science. These meanings differ from the intuition many people have when first hearing the terms. Much of the confusion in this discussion seems to come from this fact.

The terms "pass-by-value" and "pass-by-reference" are talking about variables. Pass-by-value means that the value of a variable is passed to a function/method. Pass-by-reference means that a reference to that variable is passed to the function. The latter gives the function a way to change the contents of the variable.

By those definitions, Java is always pass-by-value. Unfortunately, when we deal with variables holding objects we are really dealing with object-handles called references which are passed-by-value as well. This terminology and semantics easily confuse many beginners.

It goes like this:

public static void main(String[] args) {
    Dog aDog = new Dog("Max");
    Dog oldDog = aDog;

    // we pass the object to foo
    foo(aDog);
    // aDog variable is still pointing to the "Max" dog when foo(...) returns
    aDog.getName().equals("Max"); // true
    aDog.getName().equals("Fifi"); // false
    aDog == oldDog; // true
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true
    // change d inside of foo() to point to a new Dog instance construct red with name member variable set to "Fifi"
    d = new Dog("Fifi");
    d.getName().equals("Fifi"); // true
}

In this example, aDog.getName() will still return "Max". The value aDog within main is not changed in the function foo by creating new Dog with name member variable set to "Fifi" because the object reference is passed by value. If the object reference was passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.

Likewise:

public static void main(String[] args) {
    Dog aDog = new Dog("Max");
    Dog oldDog = aDog;

    foo(aDog);
    // when foo(...) returns, the name of the dog has been changed to "Fifi"
    aDog.getName().equals("Fifi"); // true
    // but it is still the same dog:
    aDog == oldDog; // true
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true
    // this changes the name of d to be "Fifi"
    d.setName("Fifi");
}

In this example, Fifi is dogโ€™s name after call to foo(aDog) because the object's name was set inside of foo(...). Any operations that foo performs on d are such that, for all practical purposes, they are performed on aDog, but it is not possible to change the value of the variable aDog itself.

For more information on pass by reference and pass by value, consult the following answer: https://stackoverflow.com/a/430958/6005228. This explains more thoroughly the semantics and history behind the two and also explains why Java and many other modern languages appear to do both in certain cases.

๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ can someone explain pass by value in java please?
r/java on Reddit: Can someone explain pass by value in Java please?
January 26, 2012 -

I've read multiple explanations online but I'm still having trouble understanding. I understand that Java is pass by value but from what I understand objects are passed by reference. My programming teacher even had trouble explaining this. Can someone please give me a clear explanation of this, in layman's terms, as if I had never programmed before?

Also, if someone could give some of the pros and cons of pass by value and pass by reference that would be great too, and what languages are pass by value and which are pass by reference?

Top answer
1 of 5
22
This is my favorite question about java. Its nearly impossible to answer easily because people mean different things. The real answer is Java passes by value, 100% of the time, but the value it passes for objects is the reference address of the object, not the object itself. This doesn't usually answer the question though, because what people are usually really asking is "If i pass an object to a method and perform an operation on it, will the change be evident in the original object once I exit the method?" Here's the (actual useful) breakdown: If you pass a primitive to a method, you get its value, and changing it will not change the value of the primitive you passed in. If you pass an object to a method, you get a reference (like a pointer) back to your original object. If you perform a method ON the object ( myObject.doSomething() ), the action that method takes WILL be performed on your original passed in object. This works LIKE pass by reference in other languages. If you perform an ASSIGNMENT on the object (anything with myObject = ...) then you are NOT affecting the original passed in object, you are just discarding your reference to it and replacing it with a new reference to a different object. Any operations you perform on the object after you have reassigned its reference in the method will NOT affect the passed in object, because those operations will be performed on the new object you assigned to the reference, not the original passed in reference.
2 of 5
14
http://stackoverflow.com/questions/40480/is-java-pass-by-reference
Discussions

methods - Is Java "pass-by-reference" or "pass-by-value"? - Stack Overflow
I always thought Java uses pass-by-reference. However, I read a blog post which claims that Java uses pass-by-value. I don't think I understand the distinction the author is making. What is the More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is Java pass-by-value or pass-by-reference?
Talking about pass-by-reference vs pass-by-value is kind of pointless as this is not a ubiquitous concept and the meanings vary slightly between programming languages. All that matters are the semantics in a particular language, there really is no point to ever say 'pass-by-value' or 'pass-by-reference'. Typically 'pass-by-value' means you create a copy of an object and pass it to the function - if you modify this object in the function then it will be only this copy that you modify and not the original object. Whereas 'pass-by-reference' means you pass a 'reference' to the original object and any modification will modify the object the reference is referencing. Essentially 'pass-by-value' and 'pass-by-reference' do not have the same meaning in C++ and Java. People in here saying that Java is always pass-by-value but the value passed is a reference don't understand that these are implementation details of a pass-by-reference strategy. If this is way you want to talk about it you might as well say its 'pass-by-banana' and then define what that means as whatever. More on reddit.com
๐ŸŒ r/java
52
9
January 14, 2015
How Kotlin pass variables, by reference or by value?
It works the same as Java when you run it on the JVM Edit: It is always a bit of a mindf with java but it is pass-by-value though the value is a "Java-reference" which kind of messes up your brain :P More on reddit.com
๐ŸŒ r/Kotlin
16
0
November 15, 2018
Are arrays pass by reference or pass by value?

In java except the primitive data types like int, char everything else is pass by reference is a wrong concept. Everything is pass by value. In case of objects you are passing the reference of the object to the new object . The references are passed by value. For ex arr[ ] = new int[5]; Here arr is a variable pointing to a memory location of array of int. Now consider,

Fn(int[ ] arr2){ .... }

Call fn(arr);

Here the reference of arr that is the memory location of where the array of integer starts will be given to arr2.

Hope this clears your confusion.

More on reddit.com
๐ŸŒ r/learnjava
10
14
May 27, 2020
People also ask

How is JavaScript different from Java?
JavaScript is different from Java because JavaScript is a dynamic, interpreted language primarily used for web development, running directly in browsers or on servers, thanks to runtimes such as Node.js, Bun, or Deno. Java, in contrast, is a statically typed, compiled language that runs on the Java Virtual Machine (JVM) and is popular for building large-scale applications and Android software. Despite the similarity in their names, they're completely different. Their design, execution, and typical use cases differ significantly. For more details, see this java vs javascript guide.
๐ŸŒ
roadmap.sh
roadmap.sh โ€บ java
Learn to become a modern Java developer
When is Java used vs C++?
Java and C++ are both object-oriented programming languages, but they solve problems differently. C++ gives precise control over memory, hardware, and execution. Java, on the other hand, offers portability, automatic garbage collection, and a stable environment for teams that need predictable scaling. These two programming languages remain essential for modern software because they target different development needs. Check out our Java vs. C++ guide for a full comparison between these two powerful languages.
๐ŸŒ
roadmap.sh
roadmap.sh โ€บ java
Learn to become a modern Java developer
Is Kotlin better than Java?
Choosing between Java and Kotlin isnโ€™t about finding the โ€œbetterโ€ language. Itโ€™s about picking the one that fits your specific project needs. Both run on the Java Virtual Machine (JVM) and share a rich ecosystem, but they take different approaches to solving programming problems. Check out our Java vs Kotlin comparison to know all the differences between these powerful programming languages.
๐ŸŒ
roadmap.sh
roadmap.sh โ€บ java
Learn to become a modern Java developer
๐ŸŒ
Medium
medium.com โ€บ @asli.beyhann โ€บ pass-by-value-and-pass-by-reference-in-java-an-in-depth-exploration-9c0e4534721e
Pass By Value and Pass By Reference in Java: An In-Depth Exploration | by Asli Beyhan | Medium
November 22, 2024 - Letโ€™s explore how these concepts work in Java with examples to clarify their behaviors. ... In Java, primitive types such as int, double, and byte are passed to methods by value.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ pass-by-value as a parameter passing mechanism in java
Pass-By-Value as a Parameter Passing Mechanism in Java | Baeldung
January 8, 2024 - It means that when a variable is pass-by-reference, the unique identifier of the object is sent to the method. Any changes to the parameterโ€™s instance members will result in that change being made to the original value.
๐ŸŒ
Playwright
playwright.dev โ€บ assertions
Assertions | Playwright
You can extend Playwright assertions by providing custom matchers. These matchers will be available on the expect object. In this example we add a custom toHaveAmount function. Custom matcher should return a pass flag indicating whether the assertion passed, and a message callback that's used when the assertion fails.
Find elsewhere
Top answer
1 of 16
7154

The terms "pass-by-value" and "pass-by-reference" have special, precisely defined meanings in computer science. These meanings differ from the intuition many people have when first hearing the terms. Much of the confusion in this discussion seems to come from this fact.

The terms "pass-by-value" and "pass-by-reference" are talking about variables. Pass-by-value means that the value of a variable is passed to a function/method. Pass-by-reference means that a reference to that variable is passed to the function. The latter gives the function a way to change the contents of the variable.

By those definitions, Java is always pass-by-value. Unfortunately, when we deal with variables holding objects we are really dealing with object-handles called references which are passed-by-value as well. This terminology and semantics easily confuse many beginners.

It goes like this:

public static void main(String[] args) {
    Dog aDog = new Dog("Max");
    Dog oldDog = aDog;

    // we pass the object to foo
    foo(aDog);
    // aDog variable is still pointing to the "Max" dog when foo(...) returns
    aDog.getName().equals("Max"); // true
    aDog.getName().equals("Fifi"); // false
    aDog == oldDog; // true
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true
    // change d inside of foo() to point to a new Dog instance construct red with name member variable set to "Fifi"
    d = new Dog("Fifi");
    d.getName().equals("Fifi"); // true
}

In this example, aDog.getName() will still return "Max". The value aDog within main is not changed in the function foo by creating new Dog with name member variable set to "Fifi" because the object reference is passed by value. If the object reference was passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.

Likewise:

public static void main(String[] args) {
    Dog aDog = new Dog("Max");
    Dog oldDog = aDog;

    foo(aDog);
    // when foo(...) returns, the name of the dog has been changed to "Fifi"
    aDog.getName().equals("Fifi"); // true
    // but it is still the same dog:
    aDog == oldDog; // true
}

public static void foo(Dog d) {
    d.getName().equals("Max"); // true
    // this changes the name of d to be "Fifi"
    d.setName("Fifi");
}

In this example, Fifi is dogโ€™s name after call to foo(aDog) because the object's name was set inside of foo(...). Any operations that foo performs on d are such that, for all practical purposes, they are performed on aDog, but it is not possible to change the value of the variable aDog itself.

For more information on pass by reference and pass by value, consult the following answer: https://stackoverflow.com/a/430958/6005228. This explains more thoroughly the semantics and history behind the two and also explains why Java and many other modern languages appear to do both in certain cases.

2 of 16
3628

I'm the author of the blog post you're talking about. To clarify a few things:

The Java Spec says that everything in Java is pass-by-value. There is no such thing as "pass-by-reference" in Java.

The key to understanding this is that something like

Dog myDog;

is not a Dog; it's actually a pointer to a Dog. The use of the term "reference" in Java is very misleading and is what causes most of the confusion here. What they call "references" act/feel more like what we'd call "pointers" in most other languages.

What that means, is when you have

Dog myDog = new Dog("Rover");
foo(myDog);

you're essentially passing the address of the created Dog object to the foo method.

(I say essentially because Java pointers/references aren't direct addresses, but it's easiest to think of them that way.)

Suppose the Dog object resides at memory address 42. This means we pass 42 to the method.

if the Method were defined as

public void foo(Dog someDog) {
    someDog.setName("Max");     // AAA
    someDog = new Dog("Fifi");  // BBB
    someDog.setName("Rowlf");   // CCC
}

let's look at what's happening.

  • the parameter someDog is set to the value 42
  • at line "AAA"
    • someDog is followed to the Dog it points to (the Dog object at address 42)
    • that Dog (the one at address 42) is asked to change his name to Max
  • at line "BBB"
    • a new Dog is created. Let's say he's at address 74
    • we assign the parameter someDog to 74
  • at line "CCC"
    • someDog is followed to the Dog it points to (the Dog object at address 74)
    • that Dog (the one at address 74) is asked to change his name to Rowlf
  • then, we return

Now let's think about what happens outside the method:

Did myDog change?

There's the key.

Keeping in mind that myDog is a pointer, and not an actual Dog, the answer is NO. myDog still has the value 42; it's still pointing to the original Dog (but note that because of line "AAA", its name is now "Max" - still the same Dog; myDog's value has not changed.)

It's perfectly valid to follow an address and change what's at the end of it; that does not change the variable, however.

Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, the caller will not see any changes you make to where that pointer points. (In a language with pass-by-reference semantics, the method function can change the pointer and the caller will see that change.)

In C++, Ada, Pascal and other languages that support pass-by-reference, you can actually change the variable that was passed.

If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB.

Think of reference parameters as being aliases for the variable passed in. When that alias is assigned, so is the variable that was passed in.

A discussion in the comments warrants some clarification...

In C, you can write

void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}

int x = 1;
int y = 2;
swap(&x, &y);

This is not a special case in C. Both languages use pass-by-value semantics. Here the call site is creating additional data structure to assist the function to access and manipulate data.

The function is being passed pointers to data, and follows those pointers to access and modify that data.

A similar approach in Java, where the caller sets up assisting structure, might be:

void swap(int[] x, int[] y) {
    int temp = x[0];
    x[0] = y[0];
    y[0] = temp;
}

int[] x = {1};
int[] y = {2};
swap(x, y);

(or if you wanted both examples to demonstrate features the other language doesn't have, create a mutable IntWrapper class to use in place of the arrays)

In these cases, both C and Java are simulating pass-by-reference. They're still both passing values (pointers to ints or arrays), and following those pointers inside the called function to manipulate the data.

Pass-by-reference is all about the function declaration/definition, and how it handles its parameters. Reference semantics apply to every call to that function, and the call site only needs to pass variables, no additional data structure.

These simulations require the call site and the function to cooperate. No doubt it's useful, but it's still pass-by-value.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-is-pass-by-value-and-not-pass-by-reference
Java is Pass by Value, Not Pass by Reference | DigitalOcean
December 6, 2022 - Pass by value: The method parameter values are copied to another variable and then the copied object is passed to the method. The method uses the copy. Pass by reference: An alias or reference to the actual parameter is passed to the method. The method accesses the actual parameter.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_generics.asp
Java Generics
When you call the method, Java figures out what T should be based on the argument you pass in.
๐ŸŒ
Roadmap
roadmap.sh โ€บ java
Learn to become a modern Java developer
January 27, 2026 - Once familiar with Java, start practicing developing small Java programs; they will give you a chance to face real-world problems and find creative solutions to them. While you're doing that, try to study best practices and understand development methodologies to help you build expertise. And if that wasn't enough, many aspiring developers take online training courses and gain real-world experience through entry-level roles. In the end, the best way to learn programming is by doing, and these entry-level jobs are great for that.
๐ŸŒ
Medium
ananya281294.medium.com โ€บ java-passing-by-value-or-passing-by-reference-c75e312069ed
Java: Pass By Value Or Pass By Reference | by Ananya Sen | Nov, 2020 | Medium | Medium
November 18, 2020 - Pass By Reference: The pass by reference method passes the parameters as a reference(address) of the original variable. The called function does not create its own copy, rather, it refers to the original values only. Hence, the changes made in the called function will be reflected in the original parameter as well. Java follows the following rules in storing variables:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ g-fact-31-java-is-strictly-pass-by-value
Java is Strictly Pass by Value! - GeeksforGeeks
April 2, 2024 - From the above examples of C++ and Java programs, the parameters x and y are taking the copies of the value of a and b and performing the operation based on the copied values passed to the methods. As in all high language when ever a method or function is being parameterized they will only pass the values as copies to the methods or functions. Pointers are the efficient way of passing the parameters by reference and hence the values of the parameters are mutable as the operation being performed is on the address( memory location of variable ) rather than the value of variable.
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ is-java-pass-by-reference-or-pass-by-value
Is Java "pass-by-reference" or "pass-by-value"?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
Appian
docs.appian.com โ€บ suite โ€บ help โ€บ 26.3 โ€บ understanding-the-health-check-report.html
Understanding the Health Check Report [DevOps]
Although there may be cases where the risk is low, it is often difficult to verify and maintain that low risk over time. Therefore the recommendation is to always pass CDTs by value, never by reference.
๐ŸŒ
Javadude
javadude.com โ€บ post โ€บ 20010516-pass-by-value
Java is Pass-by-Value, Dammit! | javadude.com
May 16, 2001 - The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter. Java is strictly pass-by-value, exactly as in C.
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ languages โ€บ java: what to know about passing by value
Java: What to Know About Passing by Value
November 22, 2017 - by value: when arguments are passed by value to a method, it means that a copy of the original variable is being sent to the method and not the original one, so any changes applied inside the method are actually affecting the copy version. by ...
๐ŸŒ
GitHub
github.com โ€บ goatfungus โ€บ nmssaveeditor
GitHub - goatfungus/NMSSaveEditor: No Man's Sky - Save Editor ยท GitHub
Currently supports Steam/GOG, PS4 (via Save Wizard), and MS Game Pass (Xbox). More screenshots can be found here. ... Download the latest version. Run the exe and extract the contents of the zip file to an empty folder somewhere. Run the bat file (or the jar file if the file extension is associated to java).
Starred by 2.5K users
Forked by 270 users
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-pass-by-reference-in-java
How to pass by reference in Java
Java is always a pass by value; but, there are a few ways to achieve pass by reference:
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ is java pass-by-reference or pass-by-value?
Is Java Pass-By-Reference or Pass-By-Value? | Sentry
July 12, 2022 - A simple Google search of this question will return a straightforward answer that Java is always pass-by-value.
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ is java pass-by-value or pass-by-reference?
r/java on Reddit: Is Java pass-by-value or pass-by-reference?
January 14, 2015 - If you pass an object to a function in java what you get is not a copy of the original object as what would happen in pass-by-value, but a reference to the original object with the restriction that you cannot change what the reference references.
๐ŸŒ
Jonskeet
jonskeet.uk โ€บ java โ€บ passing.html
Parameter passing in Java - by reference or by value?
Both are passed by value. This section is courtesy of Chris Smith. (Updated by Jon Skeet.) There are good reasons that Java excluded the idea of pass-by-reference from its language design, and when writing Java applications it's best to do as Java does. There are elegant solutions to all common problems that may be solved with pass-by-reference in other languages.