Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

41
  • 13
    so what happens to "Fifi" in the 1st example? Does it cease to exist, was it never created, or does it exist in the heap but without a reference variable in the stack? Commented Sep 29, 2020 at 18:03
  • 122
    To me, saying that an object's reference is passed by value is the same as saying that the object is passed by reference. I'm a Java novice, but I presume that (in contrast) primitive data is pass by value. Commented Oct 27, 2020 at 18:46
  • 29
    @user36800: You're wrong. Did you work through the example with Fifi and look carefully through the results? Check that indeed foo(aDog); did not change aDog despite foo overwriting the value of d, showing that indeed all inputs to a function are passed by value. Commented Dec 23, 2020 at 15:03
  • 24
    @user36800: Well, both statements are wrong. To pass an object by reference would mean that if the function modifies the variable then it modifies the object itself. That is not what happens in Java; objects cannot be passed by reference, but instead one can only pass references as inputs to a function, and when a function performs d = new Dog("Fifi"); it overwrites the input variable d, which stores a reference but is not 'the object passed by reference'. Contrast with &d in the function signature in C, which would be pass-by-reference. [cont] Commented Dec 28, 2020 at 5:49
  • 77
    @dbrewster i'm sorry but ... "Fifi" is not among us anymore Commented Feb 2, 2021 at 19:51