This discussion is archived
6 Replies Latest reply: Sep 9, 2012 3:16 PM by EJP RSS

Java passing by reference, where a good detailed explaination?

GiulioSeverini Newbie
Currently Being Moderated
Hi,

I am new to the forum and I have a question for you. I am trying to properly understand the Java passing by reference but I am getting a little bit confused.
Does Java pass by reference or by value?
I have a quote from James Gosling stating as following:

Some people will say incorrectly that objects are passed “by reference.” In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory…. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode — pass by value — and that helps keep things simple.

On the other hand I am studying on Thinking in Java by Bruce Eckel where it is stated that Java - apart from primitives - pass objects by reference.

So where is the truth? Where can I find a detailed explaination?

Thank you and kind regards,

Giulio.
  • 1. Re: Java passing by reference, where a good detailed explaination?
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!
    >
    On the other hand I am studying on Thinking in Java by Bruce Eckel where it is stated that Java - apart from primitives - pass objects by reference.

    So where is the truth? Where can I find a detailed explaination?
    >
    You already have the truth in what James Gosling, the creator of Java, said. You either misstated what the other reference said or it is wrong.

    What Mr. Gosling said is also represented in the Java Language Specification
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.1

    See section 8.4.1
    >
    When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor.
    >
    Note that the above applies to argument expressions of ALL types: primitive or object. The value of the 'newly created parameter variable' is a COPY (by value) of the actual argument expression.

    Here is a JavaWorld article with examples that should help clear things up.
    http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html

    ---
    Edited by: rp0428 on Sep 8, 2012 9:11 AM - added additional comment after researching OP's statement.

    Just as I suspected you misstated what the reference said. I could not find a reference that used exactly your wording
    >
    Thinking in Java by Bruce Eckel where it is stated that Java - apart from primitives - pass objects by reference.
    >
    But I did find this reference to his book
    http://books.google.com/books?id=Ql6QgWf6i7cC&pg=PA1026&lpg=PA1026&dq=thinking+in+java+bruce+eckel+pass+by+reference&source=bl&ots=0bjpdK7tg4&sig=hSQWrwS8lJKlbAMoOVPHXVF2PGc&hl=en&sa=X&ei=WW1LUNa-AsX5igLM4IHoCQ&ved=0CDwQ6AEwAw#v=onepage&q=thinking%20in%20java%20bruce%20eckel%20pass%20by%20reference&f=false

    And in that reference on page 1026 of his book he has similar words but you have taken them completely out of context.
    That section is titled 'Pass by value' and the author clearly states
    >
    there are two fairly distinct camps:

    1. Java passes everything by value.
    . . .
    2. Java passes primitive by value (no argument there), but objects are passed by reference.
    . . .
    >
    Item 2 above is presumably where you got your statement that you included in your post.

    But the author clearly states in the following paragraph that he is sidestepping the issue.
    >
    Having given both camps a good airing, and after saying "It depends on how you think of a reference," I will attempt to sidestep the issue. In the end, it isn't that important - what is important is that you understand that passing a reference allows the caller's object to be changed unexpectedly.
    >

    Edited by: rp0428 on Sep 8, 2012 9:11 AM
  • 2. Re: Java passing by reference, where a good detailed explaination?
    GiulioSeverini Newbie
    Currently Being Moderated
    Hey, wait a minute - maybe I have understood finally.

    Thing is: everything in Java is an Object, and all the objects are passed by reference. BUT, references themselves are passed by value - and not by another reference as it happens, for example, in C / C++ with the pointers when you can pass pointers by reference letting them to be modified. In Java references - pointers, let's say, are passed by value, so it means a copy of them is created and passed to a method. This method, then, can alter the copy but not the original, which remains untouched (this avoids, among other, some security issues where a virus can modify a pointer passed by reference making it point to a different object).

    Is that correct?

    Giulio.

    Edited by: Giulio Severini on 9-set-2012 3.02
  • 3. Re: Java passing by reference, where a good detailed explaination?
    EJP Guru
    Currently Being Moderated
    Java passing by reference, where a good detailed explaination?
    There is no such thing in Java, so there is no explanation either, detailed or otherwise. There is nothing to explain.
  • 4. Re: Java passing by reference, where a good detailed explaination?
    GiulioSeverini Newbie
    Currently Being Moderated
    Hey, EJP, can you please have a look at my self-answer, if it is correct or not?
    Kind regards,

    Giulio.
  • 5. Re: Java passing by reference, where a good detailed explaination?
    rp0428 Guru
    Currently Being Moderated
    >
    everything in Java is an Object, and all the objects are passed by reference.
    >
    Wrong - Objects are not passed at all so they can't be passed by reference or value.

    What is passed is a reference to an Object and as you just said
    >
    references themselves are passed by value
  • 6. Re: Java passing by reference, where a good detailed explaination?
    EJP Guru
    Currently Being Moderated
    everything in Java is an Object
    No.
    and all the objects are passed by reference.
    No.
    references themselves are passed by value
    Yes.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points