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.

Top answer
1 of 16
7153

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.

🌐
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:
Discussions

Can someone explain pass by value in Java please?
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. More on reddit.com
🌐 r/java
40
10
January 26, 2012
Is Java pass-by-value or pass-by-reference?
It is always pass-by-value; but with the addition that non-plain variables can only be references. If you're from a C++ background -- all objects are created on the heap, and every (non-plain) variable is a "pointer-to" type automatically. The following has no Java equivalent: void func(MyObject &reference, MyObject byvalue) All you've got is the equivalent of: void func(MyObject *reference) But obviously in Java, there's no "*" necessary. 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
🌐
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.
🌐
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
🌐
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.
🌐
Medium
medium.com › javarevisited › is-java-pass-by-reference-or-pass-by-value-f03562367446
Is Java ‘pass-by-reference’ or ‘pass-by-value’? | by Pravinkumar Singh | Javarevisited | Medium
July 15, 2023 - Pass-by-value: It means the value of a variable is passed to a method and when a method is called, a new copy of the variable is created in the method’s scope. Any changes made to this variable within the method don’t affect the original ...
🌐
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.
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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.
🌐
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:
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java pass by reference and pass by value with examples
Java Pass By Reference And Pass By Value With Examples
April 1, 2025 - There are basically two types of techniques for passing the parameters in Java. The first one is pass-by-value and the second one is pass-by-reference.
🌐
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
🌐
InfoWorld
infoworld.com › home › blogs › java challengers
Does Java pass by reference or pass by value? | InfoWorld
June 6, 2024 - Passing by value means we pass a copy of the value. Passing by reference means we pass the real reference to the variable in memory. All object references in Java are passed 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 - It is always pass-by-value; but with the addition that non-plain variables can only be references. If you're from a C++ background -- all objects are created on the heap, and every (non-plain) variable is a "pointer-to" type automatically. The following has no Java equivalent:
🌐
Edureka
edureka.co › blog › pass-by-value-pass-reference-java
Pass by Value and Pass by Reference in Java - Edureka
June 17, 2021 - Pass by Value: It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value. Pass by Reference: It is a process in which the actual copy of reference ...
🌐
YouTube
youtube.com › coding with john
Java is ALWAYS Pass By Value. Here's Why - YouTube
Is Java pass by reference or pass by value? Java is ALWAYS pass by value, not pass by reference. But it can look like it's pass by reference when it's not. W...
Published   August 30, 2021
Views   121K
🌐
DEV Community
dev.to › iamcymentho › java-pass-by-value-vs-pass-by-reference-unraveling-the-confusion-3gmh
Java: "Pass-by-Value" vs "Pass-by-Reference" - Unraveling the Confusion - DEV Community
August 27, 2023 - In Java, when you pass a primitive type, a copy of its value is passed to the method, leaving the original variable unchanged. When you pass an object reference, the reference's value (memory address) is copied, not the object itself.
🌐
Medium
medium.com › @barbieri.santiago › java-f09e09fc5e82
Understanding Java’s Pass-by-Value with Object References | by Santiago | Medium
February 24, 2024 - Java always passes arguments by value, not by reference. This means that when you pass a variable to a method, you are passing the value of the variable. For primitive types, this is the actual value (e.g., int, double).
🌐
Scaler
scaler.com › home › topics › pass by value and call by reference in java
Pass by Value and Call by Reference in Java - Scaler Topics
February 11, 2022 - When we pass only the value part of a variable to a function as an argument, it is referred to as pass by value. In this case, any change to the value of a parameter in the called method does not affect its value in the calling method.