Re: no pointer in Java => my problem

From: Dale King (kingd[at]tmicha[dot]net)
Date: 04/20/04


Date: Mon, 19 Apr 2004 17:33:05 -0500


"Bryan Castillo" <rook_5150@yahoo.com> wrote in message
news:1bff1830.0404161631.3fcd58a9@posting.google.com...
> Daniel Sjöblom <dsjoblom@mbnet.fi_NOSPAM> wrote in message
news:<407ef28b$0$21236$7b6a8dc4@news.mbnet.fi>...
> > rowla wrote:
> > Java and C are both pass by value only. You can't change the value of a
> > passed pointer in a C function either (that is, it won't affect the
> > pointer from the view of the caller).
>
> I have had a few people say this (C is only pass be value) because the
> value
> you are passing is an adress and you can't change the address. That
> argument
> doesn't make sense to me. Would you say only that C++ has something
> other than pass by value? I think most C++ implementations will still
> pass the value as an adrress when passing a reference, so in reality
> you are still passing by value.

Here is an explanation I prepared a long while back on this subject and I
guess its about time to repost it:

Question:
  Does Java pass objects by reference or by value?

Answer:
  Since it makes no sense to begin any argument without agreed upon
  definitions let's formally define our terms. I will use abstract
  pseudocode to keep the issue from being clouded by the idiom of a
  particular language. The source of my information is the book
  "Advanced Programming Language Design" by Raphael A. Finkel.

  For those unfamiliar with the term below an L-value is an expression
  that can appear on the left side of an assignment statement. It is
  basically a way to address where a variable is stored. Variables
  and other ways to refer to locations in memory are L-values. Most
  expressions are not L-values, e.g. ( x * 2 )

  We assume the presence of a procedure named f that takes a formal
  parameter s. We call that function giving it an actual parameter g.

  The calling code:
    f( g )

  The function:
    procedure f( s )
    begin
      -- body of the procedure
    end;

  There are several parameter passing semantics that have been
  proposed or used:

  value
    The value of the actual parameter is copied into the formal
    parameter when the procedure is invoked. Any modification of
    the formal parameter affects only the formal parameter and
    not the actual parameter. This is the most common form of
    parameter passing and is the only one provided in C and Java.

  result
    The value of the formal parameter is copied into the actual
    parameter when the procedure returns. Modifications to the
    formal parameter do not affect the formal parameter until the
    function returns. The actual parameter must be an L-value. It
    is usually invalid to pass the same L-value to more than one
    result parameter, but the compiler cannot always detect this.
    The best example of this is out parameters in CORBA.

  value result
    Combination of value and result semantics. The best example of
    this are inout parameters in CORBA.

  reference
    The L-value of the formal parameter is set to the L-value of the
    actual parameter. In other words, the address of the formal
    parameter is the same as the address of the actual parameter. Any
    modifications to the formal parameter also immediately affect the
    actual parameter. FORTRAN only has reference mode (expressions are
    evaluated and stored in a temporary location in order to obtain an
    L-value). C++ has reference parameters by putting a & before the
    formal parameter name in the function header. Reference mode can
    be simulated in C using pointers and adding the & to the actual
    parameter and dereferencing the formal parameter within the
    function.

  readonly
    Can use either value or reference mode, but modification of the
    formal parameter is forbidden by the compiler.

  macro
  name
    These two have been used in the past, but are very much out of favor
    because they are confusing and difficult to implement. Therefore I
    won't bother trying to explain them.

  Now that we have some definitions of terms we can return to the question.
  Does Java pass objects by reference or by value?

  The answer is NO! The fact is that Java has no facility whatsoever to
  pass an object to any function! The reason is that Java has no variables
  that contain objects.

  The reason there is so much confusion is people tend to blur the
  distinction between an object reference variable and an object instance.
  All object instances in Java are allocated on the heap and can only be
  accessed through object references. So if I have the following:

    StringBuffer g = new StringBuffer( "Hello" );

  The variable g does not contain the string "Hello", it contains a
  reference (or pointer) to an object instance that contains the string
  "hello".

  So if I then call f( g ), f is free to modify its formal parameter s to
  make it point to another StringBuffer or to set it to null. The function
  f could also modify the StringBuffer by appending " World" for instance.
  While this changes the value of that StringBuffer, the value of that
  StringBuffer is NOT the value of the actual parameter g.

  Imagine for instance if I set g to null before passing it to f. There is
  no StringBuffer now to modify and f can in no way change the value of g
  to be non-null.

  A real world analogy would be if I had a piece of paper with an address
  on it and I copied that address onto another piece of paper that I gave
  you. That piece of paper is not the house at that address it is only
  a reference to that house. There could of course be several pieces of
  paper with that address on it. You could then go to that house and
  modify it by painting it blue, but that does not change the piece of
  paper I gave you. You could change the address on your paper, but that
  does not change the address on my paper. You cannot change my paper,
  since we assume that you don't even know where my paper is or really
  even the existance of my paper. That is pass by value. I gave you the
  value of my paper.

  It's a bit harder to come up with a good real world analogy for pass by
  reference. If I gave you my piece of paper which you hand back to me when
  you are done (possibly changing the address), that would be like value
  result mode. Result mode would be me handing you a blank piece of paper
  on which you write the address and return to me.

  The bottom line is Java only has variables that hold primitives or object
  references. Both are passed by value.



Relevant Pages

  • Re: call by reference
    ... We started learning Java about one month ago, and they just don't believe me when i try to explain that there is a confusion when passing reference types as an argumetn to a function. ... The actual parameter must be an L-value. ...
    (comp.lang.java.programmer)
  • Re: Q: Pass obj by reference, manupulate and access modified obj?
    ... The method can modify the object that is referenced. ... Does Java pass objects by reference or by value? ...
    (comp.lang.java.help)
  • Re: Weird Behaviour
    ... > reference equates to a reference. ... I'm not Jon, but I'll answer. ... formal parameter is the same as the lvalue of the actual parameter. ... It's really an unfortunate coincidence that Java has these things called ...
    (comp.lang.java.help)
  • Re: Pass by reference / pass by value
    ... Also mentioned, but perhaps not quite as clear, is that Java doesn't pass objects at all, only references to objects. ... variable of type *reference to Foo* or an array of *references to Bars*. ... With pass by value, at the line marked above, all that happens is a reference to the new StringBuffer is assigned to the local variable "sb", which happens to have been initialized with the StringBuffer reference passed by the caller. ...
    (comp.lang.java.programmer)
  • Re: How java passes object references?
    ... Which everybody knows, it doesn't in Java. ... The reason I think this is a useful clarification is that when you got to the part about how passing by reference might work, it seems you went off track at least partly because you didn't understand the nature of the above. ... is a pointer pointing at the memory block. ... Assignments to local variables, or even to class members, do not allocate memory. ...
    (comp.lang.java.programmer)