Skip to main content
deleted 6 characters in body
Source Link
Captain Man
  • 7.9k
  • 6
  • 55
  • 83

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. ThisThis terminology and semantics easily confuse many beginners.

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 waswas passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.

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.

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.

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.

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.

grammatical clarity explaining causation less ambiguously
Source Link
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 thethis example above, aDog.getName() will still return "Max". The value aDog within main is not changed in the function foo with theby creating new Dog with name member variable set to "Fifi" asbecause the object reference is passed by value. If it werethe object reference was passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.

In the abovethis example, Fifi is the dog'sdog’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.

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 "Fifi"
    d = new Dog("Fifi");
    d.getName().equals("Fifi"); // true
}

In the example above aDog.getName() will still return "Max". The value aDog within main is not changed in the function foo with the Dog "Fifi" as the object reference is passed by value. If it were passed by reference, then the aDog.getName() in main would return "Fifi" after the call to foo.

In the above example, Fifi is the 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.

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.

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.

removed redundant word
Source Link
ACV
  • 10.7k
  • 5
  • 83
  • 91

The terms "pass-by-value" and "pass-by-reference" are 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 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" 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.

Add note about terms in computer science
Source Link
Lii
  • 12.2k
  • 9
  • 69
  • 92
Loading
Clarified the definition of the terms "pass-by-value" and "pass-by-reference"
Source Link
fishinear
  • 6.4k
  • 3
  • 42
  • 92
Loading
Added reference to an excellent SO answer that goes into a considerable depth on the meaning and history of pass by value vs pass by reference.
Source Link
root
  • 362
  • 1
  • 2
  • 11
Loading
Rewrite of the leading paragraph which was very confusing.
Source Link
Gray
  • 117.2k
  • 24
  • 305
  • 360
Loading
added 16 characters in body
Source Link
Deduplicator
  • 45.9k
  • 7
  • 73
  • 125
Loading
restored reference to Java object-handles being called references and that contributing to any newbies confusion
Source Link
Deduplicator
  • 45.9k
  • 7
  • 73
  • 125
Loading
Bounty Awarded with 100 reputation awarded by Javier G.Raya
Made more clear that the variable aDog keeps referencing the same object.
Source Link
fishinear
  • 6.4k
  • 3
  • 42
  • 92
Loading
Removed opinion statement that wasn't helpful to the answer
Source Link
Gray
  • 117.2k
  • 24
  • 305
  • 360
Loading
removed comment that didn't match the other true/false patterns
Source Link
Gray
  • 117.2k
  • 24
  • 305
  • 360
Loading
Made use of spaces more consistent and removed link to external code, as it has already been integrated into the question a long time ago
Source Link
Georg Plaz
  • 6k
  • 5
  • 43
  • 66
Loading
Changed `FiFi` to `Fifi` in last paragraph to be consistent with code
Source Link
Tur1ng
  • 853
  • 10
  • 23
Loading
Bounty Awarded with 50 reputation awarded by madhead
More consistent code example
Source Link
janssen-dev
  • 2.8k
  • 2
  • 31
  • 55
Loading
added 7 characters in body
Source Link
Shanta
  • 262
  • 1
  • 4
  • 15
Loading
added 1 character in body
Source Link
Ruben Helsloot
  • 13.2k
  • 6
  • 33
  • 56
Loading
Minor edit: Rearranged closing conditional braces so they don't appear as comments.
Source Link
Loading
added 467 characters in body
Source Link
Gray
  • 117.2k
  • 24
  • 305
  • 360
Loading
Using 'newbies' feels very derogatory
Source Link
pingul
  • 3.5k
  • 3
  • 29
  • 47
Loading
deleted 91 characters in body
Source Link
blackpen
  • 2.4k
  • 15
  • 15
Loading
deleted 2 characters in body
Source Link
Sohil
  • 562
  • 7
  • 26
Loading
Explained about second example
Source Link
SimpleGuy
  • 2.9k
  • 5
  • 32
  • 51
Loading