pass by reference

A

Andreas Leitgeb

Peter Duniho said:
Those who would treat the two phrases as equivalent are not being suitably
precise enough to unambiguously describe code. It's fine if you want to
use that sort of language at a cocktail party. But when you're trying to
get actual work done, it's not appropriate.

I pick my (human) language depending on who I talk with.

If the topic is, how parameter passing actually works, then I admit
that "passing references by value" is the more accurate description.

If I were to describe a method like: public void foo(String x) {...}
I'd probably start with "it takes a String and returns nothing".
Would you say: "It takes a reference to a String" ? If so, then
ten points for exactness, and minus hundred points for a dry mouth
from over-repeating the word "reference" :)

There are times for exact language, and times where focus is on
other aspects. That's the other level I've kept mentioning.
But if you want to get any real work done, you need to use your
language more carefully and more precisely than that.

If I want the work done, I assume that my colleagues know Java,
and know how Objects are handled and their pointers passed around,
so we simplify our language, strip it of unproductive cruft, and
talk about passing Objects around, rather than wasting breath for
saying "reference" twenty times per minute.
 
A

Andreas Leitgeb

If you only look on Java in isolation there is no point in discussing "pass
by value"/"pass by reference." Java only does one and the "Java idiom" of
(Java) reference types means that the programmer doesn't have to think about
it.

You're right. In that case I really speak of "passing objects" omitting
any "by ...". This is also frowned upon by others, but is a lingual
shortcut for what really happens, namely "passing a reference to the object
by value".
It's only when Java is discussed in general as a programming language or
when it is compared to another programming language that it makes sense to
talk about "pass by value"/"pass by reference" in relation to Java. It is
not a Java term. It's a _programming language_ term, describing how
arguments are passed to subprograms. It's a pretty low level term -- it has
no meaning "one level up."
I still follow you...
Yes, Java does automatic dereferencing and uses _reference types_ as the
arguments to its subprograms (along with fundamental types, of course.)

Here, I'm lagging somewhat behind. Where does java "automatic deref"?
Do you mean because method names are appended after a ".", and not after
a "->" as in C with pointers?
It's my point, that java does not auto-deref, but that java's "." is
just C's "->", so the deref is really explicit, but hidden behind a
concise syntax.
Merely being able to modify objects in a subprogram does not mean you have
pass by reference.

Ok, I think you hit the nail on the head. My position is, that C++'s
"call by reference" is actually *very* similar to Java's way of reference
passing: there are only two differences:
- C++ can also do "by-ref" with primitives, Java can not.
- C++'s assignment to a reference(aka.: "name-alias") is always a
modification on the referenced Object, never on the reference.

Even in C++, I'm "merely able to modify objects in a subprogram", but in C++
it's still called "pass by reference".

Back to Java:
...
myMethod(final Object foo) {...}
...
Whatever legal things I do with foo inside myMethod will be reflected
outside. Is *that* "call by ref" then? "foo" inside myMethod is now
just as (un-)retargettable as a reference in C++ is.
 
P

Patricia Shanahan

Andreas said:
I pick my (human) language depending on who I talk with.

Remember you are discussing the subject "pass by reference" in a
newsgroup that includes people who are trying to learn how Java
parameter passing behaves.

Patricia
 
A

Andreas Leitgeb

Patricia Shanahan said:
I came to my current position on this subject after viewing many
articles reflecting parameter passing confusion, in this newsgroup and
in comp.lang.java.help. I came to the conclusion that talking about pass
by reference created a not unreasonable expectation of being able to
change the value of a variable,

This was, because "pass by reference" alone is indeed misleading.
I always wrote "passing *objects* by reference".

Perhaps I should have used "by use of" rather than just "by", to
avoid collision with that technical definition.
What do you think is the main objective in selecting terminology to
describe Java parameter passing?

The objective is, to explain some effect using a language that I think
the target knows. Natural language appears to me superior to a language
of technical terms with exact definitions from which only experienced
programmers can grasp all the subtleties.
 
S

Stefan Ram

Patricia Shanahan said:
Remember you are discussing the subject "pass by reference" in a
newsgroup that includes people who are trying to learn how Java
parameter passing behaves.

Let me add two quotations to this thread:

»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.«

James Gosling, The Java Programming Language, 3rd Edition, quoted from

http://forum.java.sun.com/thread.jspa?threadID=591738&messageID=3087303

»call by reference A call in which the calling module
provides to the called module the addresses of the
parameters to be passed.« (15.06.08)

»call by value A call in which the calling module provides
to the called module the actual values of the parameters
to be passed.« (15.06.09)

ISO 2382-15, Information technology-Vocabulary-Part 15: Programming languages

What the quotation from ISO 2382-15 calls »parameters« is
being called »arguments« in Java.
 
P

Patricia Shanahan

Andreas said:
This was, because "pass by reference" alone is indeed misleading.
I always wrote "passing *objects* by reference".

Adding the word "objects" makes no sense at all, because there may not
be any object. Null references can be perfectly valid actual arguments.
The objective is, to explain some effect using a language that I think
the target knows. Natural language appears to me superior to a language
of technical terms with exact definitions from which only experienced
programmers can grasp all the subtleties.

Java learners can be divided, for this purpose, into two sets, those
that already know what "pass by reference" means and those that don't.

For those that don't, "pass by reference" and "pass reference by value"
are both new language, and they might as well be told how Java really
behaves.

The issue is more critical for people for whom "pass by reference" is
familiar language. They will think they know what it means, and have to
find out somehow that it means something entirely different, when
talking about Java, that it does when talking about other languages.

Patricia
 
C

Chris Smith

Andreas said:
Perhaps I should have used "by use of" rather than just "by", to avoid
collision with that technical definition.

Two comments:

1. In general usage, it's entirely normal to talk about passing an object
to a method. If more precision is required, then someone will initiate
that level of precision by making a more explicit statement involving
references.

2. This thread, in particular, is *about* pass by reference, so the
discussion here is quite precise about exactly what is happening. Don't
think that's the norm. Of course, if you use the phrase "pass by
reference" or something that sounds nearly like it, there will probably
be corrections since that phrase has a specific meaning. But in general,
there is no language police around to correct you every time you talking
about passing a Date object to some method. The more precise language
may be brought up only if someone feels there is confusion about what's
going on at a lower level.
The objective is, to explain some effect using a language that I think
the target knows. Natural language appears to me superior to a language
of technical terms with exact definitions from which only experienced
programmers can grasp all the subtleties.

Hmm. Presumably, everyone here has at least some desire to become an
experienced programmer, if they aren't already.
 
C

Chris Smith

Lew said:
No, it is not call by reference.

In particular, the call by reference (with objects as parameters) mental
model still doesn't explain what happens when null is passed for the
parameter. So instead of one simple and semantically correct semantic
model, you have to remember to think of it as call by reference, but with
some special cases for null, and only if the parameter is final, except
on the third Sunday after a blue moon...
 
L

Lasse Reichstein Nielsen

Andreas Leitgeb said:
We're only discussing human language here, not Java's features.

a) Does anyone disagree, that "pass an Object by reference" and
"pass the reference to an Object by value" refer to the same
technical feature?

Yes!
"pass ... by reference" means passing an l-value. It allows
changing the value of *a variable* that is passed by reference.

"pass ... by value" means passing an r-value.

In either case, if the value of the passed variable/the passed value
is a pointer or (Java) reference, you can change the data that it
points to/object that it references. That is separate from whether
you can change the variable itself.
b) Does anyone disagree that "pass a reference by reference" is an
entirely different feature that is not directly supported by
java, but can be effectively mimicked with arrays and/or Holder-
classes?
Reasonable.


Would anyone care to point out any difference of the two concepts in
"a)" without mixing it up with the concept in "b)" ?
Done?


This is also "pass ... by reference", but with C#'s meaning of references,
whereas Java's "pass Objects by reference" is based on java's "references".
Therefore "pass by reference" means something different depending on
the language context.

Disagree. "Pass by reference" (or really "call by reference") is a
technical term with a *long* history (e.g., "Call-by-name,
call-by-reference and the Lambda calculus", Reynolds 72). It has not
been given a different meaning in a Java context. Or, if it has, the
term is now ambiguous and should be avoided.
exactly that: "pass a reference by reference" :)

If "passing a reference" is the same as "passing an object by
reference", then "passing a reference by reference" would be the same
as "passing an object by reference by reference"? I think not :)

/L
 
A

Andreas Leitgeb

Patricia Shanahan said:
The issue is more critical for people for whom "pass by reference" is
familiar language. They will think they know what it means, and have to
find out somehow that it means something entirely different, when
talking about Java, that it does when talking about other languages.

By the way, this is also true for "reference" itself. They also have
to learn that what Java calls a reference wouldn't be called that way
in other languages, for exactly the reasons you wrote (null allowed,
assignment-semantics).

Why is it ok, to talk of references in Java, but have an entirely
different meaning of "reference" in mind in context of the phrase
"pass by reference"?

I deduced my liberty to "redefine" "pass by ref" from Java's liberty
to redefine "reference" in the first place. My redefinition of
it was coined on java's vocabulary: Objects are not passed directly
(as possible in C++, where the whole structure of the object gets
dumped on the stack), but "through use of (Java-)references".
This is natural language as I understand it.
 
A

Andreas Leitgeb

Chris Smith said:
2. This thread, in particular, is *about* pass by reference, so the
discussion here is quite precise about exactly what is happening.

You're right here. I missed that drift.
there is no language police around to correct you every time you talking
about passing a Date object to some method. The more precise language
may be brought up only if someone feels there is confusion about what's
going on at a lower level.

100% agree. I failed to notice, that the thread was nearer to the
technical than to the informal "level".

I'll reread the article to which I first followedupto in this Thread, and
see if I see it differently now, on rereading.
Hmm. Presumably, everyone here has at least some desire to become an
experienced programmer, if they aren't already.
"desires to become".equals("is") == false :)
 
A

Arved Sandstrom

Chris Smith said:
Andreas Leitgeb wrote: [ SNIP ]
By the (at least as "very") definition of "reference" alone, one might
expect different behaviour of Java already. No matter if it is passed or
not. Java doesn't really have "references" the way C++ has.

I'm not sure what C++ has to do with this.

There really isn't a widely used definition of a reference. The word is
used differently by different languages, and it's best to just adopt the
definition from the language in question. C++ means one thing; Java
another; CAML yet another. On the other hand, pass-by-reference is a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
concept with a well-understood definition.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Namely: the lvalue of the
formal parameter is the same as the lvalue of the actual parameter.
[ SNIP ]

Apparently not so well-understood, Chris, not if we (and tens of thousands,
and hundreds of thousands, and millions, of others) are having this
discussion. I spent the past few days doing some fairly intense research
(Googling) on pass by reference, pass by value (also keywords "call by ...",
"pointer", "evaluation strategy", etc etc) and so forth. I tried to
categorize sources as summary and explanatory: "summary" sources had a quick
definition of a term used in this thread, and "explanatory" sources actually
had some discussion of the topics. Naturally I also applied some measure of
quality control, without trying to apply a personal bias based on my own
perspective (which has already been expressed).

(Note: I'll use PBV and PBR to abbreviate "pass by value" and "pass by
reference".)

And the fact is, "call/pass by value/reference" is _not_ something with a
well-understood definition, not when it comes to categorizing things like
"pass pointer by value". Like it or not, while people do understand PBV to
mean copying the value of the actual argument into the formal parameter
variable, they (myself included) also make a difference between that actual
argument being the thing of interest (a primitive or object) or being a
reference to the thing of interest.

I'd like to think that most folks, including everyone participating in this
thread, grasps the difference between passing a pointer by value and passing
a reference...the swap() function example, so to speak. In one you can
actually change the value of the thing passed as it visible in the caller,
and in the other you cannot. Yes, you can use pointers to modify the value
of the thing pointed at, in the calling code, but that's still not modifying
the actual parameter passed (the pointer) as it exists in the calling code.

However, lots of people (again, myself included) do describe "passing a
pointer by value" as PBR, insofar as being able to modify the pointed-to
thing is very often described as PBR. And the reason this terminology is
common because when passing a pointer by value you do in fact have a
reference to the object. I'm trying not to be argumentative here, I'm just
pointing out (pun not intended) that there are at least two different usages
of the word "reference" here, one somewhat less technically precise than the
other, but both are (arguably) valid. It depends on the context.

I myself am perfectly happy to use the more precise language to keep Java
people happy - it's no sweat off my back. But equally well I'm not going to
correct a C programmer who says "pass by reference" (when it's "pass pointer
by value") when he clearly knows what it is he or she is doing.

AHS
 
A

Andreas Leitgeb

PreScriptum: By now, I've read most of the later posts, and my
focus has moved from insisting to use "by ref" in java-context
to understanding the technical difference in detail.

Chris Smith said:
... Pass by reference is a concept that has a particular meaning,
independent of any other use of the word "reference" in a language.
[...]

Then either the word "reference" also needs a particular meaning, or
the "pass by ref" is a cyclic definition, based on a meaning of
"reference" that is derived from that concept...

Most seem to agree to that whatever a reference actually is, it must
be one that references the *variable* (or lvalue) that was passed to
it. Also the non-null-ness of a reference seems to be quite agreed
on here.

I want to understand the crucial difference between C++'s
way and Java's way in those special situations, where their
technical effect coincides. There are of course other situations
where the difference is obvious, but I want to understand those
where it isn't.

If in C++ I've got a reference-parameter in a function f1, and pass this
reference(which refers to a real variable one call-level upstairs)
to another function f2 (again, by ref), then obviously f2 may modify
the actual variable two levels up (which was passed to f1), and *not*
modify the *reference* that lives in f1 and still refers to the actual
object "var" in the upper level, afterwards.
If there's a thinko in this paragraph, please point it out.
If it's just unclear, see the following C++ code snippet.

struct mystruct { int i; } var;
void f2(mystruct& x2) { x2=*(new mystruct()); }
void f1(mystruct& x1) { f2(x1); } // <--- x1 is not changed, only var is.
int main() { f1(var); }

* Is the call to f2 from within f1 really "by reference" ?
* Is "x1" (as argument to f2) actually an lvalue ?

If "x1" is just "seen" as an lvalue identical to "var", then
why cannot a java ref be "seen" as "identical" to the object
it points to? Just because it can be null?

f1(*(mystruct*)0); // is compileable! the inevitable runtime SIGSEGV
// only happens inside f2 at the assignment,
// f1 has no problem with a null-reference.

Just because Java-refs can be re-targetted by assignment?
Java's "." operator corresponds to C's "->" or: jRef.xyz == (*cPtr).xyz
If Java had an operator to tell an Object (addressed through the java-ref)
to assume another object's value (as happens when assigning to a C++
reference), would it do "pass by ref", then? IOW: does the difference
lie in the semantics of assignment?
i.e., a reference in C++ is simply another symbol, with a possibly different
scope, that denotes the same object as the symbol it is initialized from.

int x[10]; for (int i=0; i<10; i++) { int &j=x; j=42; }
Ref j doesn't need to be initialized from a symbol.

As I've tried to show, most of the artefacts that are used to
disprove java's passing by reference, can be mimicked in C++
as well. The resolution of this contradiction can be either,
that "passing by ref" depends on more than just a method's
signature, or, that Java at least approaches some effects of
pass-by-ref quite closely.
 
A

Arved Sandstrom

Peter Duniho said:
[...]
There are times for exact language, and times where focus is on
other aspects. That's the other level I've kept mentioning.

I really don't understand how you can, in a thread named "pass by
reference", think that using the imprecise form of communication makes any
sense whatsoever.

Mainly because very early in this thread one poster very clearly said:

"In java, all objects are passed by reference.

Basic data types like int, long, boolean are passed by value. Object
variables are always references to an object, and the reference is passed
"by value" as the basic data types, but it is a reference, so..."

Note the first sentence. You certainly can't say Java objects are passed by
value either.

Point being, and I made another recent post to this effect, very many people
do understand the distinction between pass by value and pass by reference,
but also call "passing a pointer by value" passing by reference. You can
call this imprecise...me myself, if I am satisfied that the person actually
understands that they are passing a pointer by value, and not a true
reference, don't lose sleep if they use the term "pass by reference".
Because what they mean is that they have a reference (a
pointer/handle/thingamajiggy) that points to the thing they want to access.
Not a C++-style reference, but a reference.

But when talking to Java folks I'll be very careful from now on. :)

AHS
 
A

Andreas Leitgeb

Andreas Leitgeb said:
PreScriptum: By now, I've read most of the later posts, ...

Sidenote: of course I didn't travel to future :)
I meant "later than this somewhat aged posting that I
was answering only now"...
 
A

Arved Sandstrom

Stefan Ram said:
Patricia Shanahan said:
Remember you are discussing the subject "pass by reference" in a
newsgroup that includes people who are trying to learn how Java
parameter passing behaves.

Let me add two quotations to this thread:
[ SNIP Gosling's quotation ]
»call by reference A call in which the calling module
provides to the called module the addresses of the
parameters to be passed.« (15.06.08)

»call by value A call in which the calling module provides
to the called module the actual values of the parameters
to be passed.« (15.06.09)

ISO 2382-15, Information technology-Vocabulary-Part 15: Programming
languages

What the quotation from ISO 2382-15 calls »parameters« is
being called »arguments« in Java.

This is all well and good. In practise what happens is that very many
programmers also take the case in which the parameter is a pointer, and the
value of that pointer is passed by value, to be call by reference. Not all
of those programmers understand that it's not call by reference in the true
sense, but enough of them do.

It's simply that there are several usages of the word "reference", and you
can't say that either is incorrect. In one sense a "reference" is a
handle/pointer to the thing of interest. IOW, I am calling by value, but I
still have a "reference" to the primitive/object that I wish to access
(including modify in the caller).

So far in this thread I haven't seen very many people contradict the fact
that Java is passing everything by value, in a technical sense. Some of us
are merely pointing out that one is still passing a "reference" to the
object...even if it is a copy of a pointer.

AHS
 
A

Andreas Leitgeb

Lew said:
Then we had really be extra careful not to spread misinformation, such as the
canard that "call by reference" is somehow equivalent to "call by value with
references".

I once came over to Java from C++. (and still do much in C++, even now)

I have C++-syntax in mind, like:

void foo(mystruct& r) {
if (r.field1) r.field2=42; else r.method(84);
otherfoo(r);
}

and when seeing Java source like:

class ... {
void foo(MyStruct r) {
if (r.field1) r.field2=42; else r.method(84);
otherFoo(r);
}
}

there is *some* temptation to think in C++ reference terms,
and accept reference-assignment as yet another exception to the
similarity (besides different library and other differences in
syntax), rather than as the killer evidence against my mental
model.
Otherwise we do a huge disservice to those who wish to become
(good) experienced programmers.

To me, this "canard" helped me quickstart into java. Only later
did I see the other mental model that explained assignment more
cleanly, but made my brain ache when I found myself losing my
grip on the algorithms for only thinking of "reference to this
and reference to that" instead of just "this and that".

Different mental models just happen, and sometimes alternative
models are not necessarily evil. Not more evil than different
human languages are, despite all the translation problems.
 
A

Arved Sandstrom

Patricia Shanahan said:
Adding the word "objects" makes no sense at all, because there may not
be any object. Null references can be perfectly valid actual arguments.


Java learners can be divided, for this purpose, into two sets, those
that already know what "pass by reference" means and those that don't.

For those that don't, "pass by reference" and "pass reference by value"
are both new language, and they might as well be told how Java really
behaves.

The issue is more critical for people for whom "pass by reference" is
familiar language. They will think they know what it means, and have to
find out somehow that it means something entirely different, when
talking about Java, that it does when talking about other languages.

Patricia

I certainly won't argue with any of the above. My first mainstream language
was FORTRAN, and the second was C. C++ came considerably later, and Java
much later than that. When using C (before any exposure to C++ or Java) I
and most everyone I knew used the term "pass by reference" when we were
doing "pass pointers by value", and by the very nature of what one normally
did with these pointers to the caller variables, there wasn't any confusion.

When first exposed to C++, I'll freely admit that for quite some time I
thought of references as "fixed pointers". Again, given what I did with
code, that concept never caused me problems. I knew I could change the state
of the referent inside the called code, and have that change be visible in
the calling code, and so forth.

Finally, when first exposed to Java (JDK 1.0.2) about ten years ago, I will
again freely admit that I thought of object references as "fixed pointers".
Since I normally don't write swap() functions, I don't think I for the
longest time ever really considered the difference between pass by reference
and pass pointer by value. Since in hindsight I almost always simply wanted
access inside the callee to the actual variable in the caller...something
that I did with passing pointers by value in C, either pointers by value or
references in C++, and object references by value in Java.

Pertaining to your last paragraph, I don't think it was until this same
subject (and also the "does Java have pointers?" subject) came up because of
Java, quite some years ago, that I truly consciously thought about the exact
differences. When I did understand the nuances, I can't honestly say that it
changed anything...I hadn't been running into problems with either C, C++ or
Java anyway. I don't recall that referring to "pass pointer by value" or
"pass object reference by value" as "pass by reference" ever caused me any
problems, anyhow. :)

I don't have any problems with conceding that a more precise terminology is
best in a general discussion. It's just that a programmer who uses the term
"pass by reference" may be fully aware that they are "passing pointers by
value", and provided that he/she and his/her correspondents are on the same
sheet of music, I personally have no issue with that turn of phrase.

AHS
 
S

Stefan Ram

Arved Sandstrom said:
It's simply that there are several usages of the word "reference", and you
can't say that either is incorrect.

In Java, I deem usages correct that agree with the definitions
and usages of the Java Language Specification, Third Edition.

If they do not, I also might deem them correct if the author
has given his deviant definition.

So they line is not drawn between one usage or another usage,
but between people who are being able and willing to give a
definition and cling to it and people who are not.

If someone does use the term in a meaning different from the
meaning imposed by the Java Language Specification, Third
Edition and does not give his deviant definition - how should
one know what he intends the term to mean then, unless one is
able to read minds at distance?
 
A

Andreas Leitgeb

Patricia Shanahan said:
"pass by reference" (or more usually "call by reference") is a long
standing term with a meaning independent of the use of "reference" as a
freestanding term. I learned it long before I knew any language in which
one could actually have a reference.

How many of the current java newbies do you think share your history
of learning and previous knowledge?

How many, in contrast, come from C++, and see the syntactical
similarity (except for assignment) between Java's way of passing
around references to Objects and C++'s way of passing objects
(among other stuff) by reference?
I don't see any reason why having to learn a Java-specific meaning
of reference should require people to learn special meanings of other
terms,

Because a substantial part of them doesn't know that general overall
meaning of "pass by ref", and thus take it literally and thusly
automatically in context with the "references" at hand in java.

An archaic definition of the phrase without also a definition for
the terms used in both the phrase and its definition, isn't all
that shiny nowadays. Perhaps just like a green on black 80x24
vt100 doesn't have the same exciting flair to everyone.
especially when with have a perfectly good phrase,
"pass reference by value", that is only one word
longer and says *exactly* how Java behaves.

Because this redirects mental focus from Objects to the references,
which I consider a less worthy goal, than you seem to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top