call by reference

P

Pete Barrett

You have a point, but if saying "Java has call by reference" makes
them think they can change passed-in object references as well as
the objects to which they point -- since this would be possible in
other languages that have something called "call by reference" --
well, that's not so good.
Well... do they? I guess you can ask them tomorrow. said:
This sounds good too, but I know that many things about Java made more
sense to me when I understood that Java has two kinds of variables,
primitives and references, and if you declare var1 as "Object var1",
var1 is not an Object *but a reference to an Object*. Once I started
thinking in this way, a lot of stuff that had previously seemed
strange ("new Object[10]" doesn't actually create 10 Objects, e.g.,
and "Object var2 = var1" doesn't make a copy of var1) suddenly made
perfect sense.

"YMMV", maybe. Or maybe this isn't the kind of thing you meant when
you said that HLLs are supposed to shield us from details.

It is what I meant, but you have a point as well - namely, that
someone who understands the language at the higher level expects
specific behaviour, and when it actually behaves differently, a
knowledge of the lower level behaviour helps in understanding why.

Nevertheless, I think that the fact that it does help is an
imperfection in the design of Java (and pretty well all other
programming languages for that matter). Ideally, the behaviour of a
(high level) programming language should be defined, and be
understandable, without reference to the implementation (and to be
fair to Java, it does try).


Pete Barrett
 
C

Chris Smith

Pete Barrett said:
Nevertheless, I think that the fact that it does help is an
imperfection in the design of Java (and pretty well all other
programming languages for that matter). Ideally, the behaviour of a
(high level) programming language should be defined, and be
understandable, without reference to the implementation (and to be
fair to Java, it does try).

I don't think that the distinction between objects and references really
does fall into the category of implementation. The low-level
implementation changed drastically between the Sun reference
implementations of versions 1.1 and 1.2, and this had no visible effect
on program behavior aside from improving performance. The JLS does
define a consistent behavior that was maintained despite a drastic
change.

We need to distinguish between problem domain and solution domain here.
Languages become better by moving the solution domain closer to the
problem domain. However, it's fundamental to problem domains that we
make distinctions between whether we're using the same entity or a
different one. You seem to have ambitions to raise programming
languages to such a high level that these problem-domain distinctions
are not representable in the solution space. I have to look at that as,
at the very least, counterproductive to solving real-world problems.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

Patricia Shanahan

Chris said:
I don't think that the distinction between objects and references really
does fall into the category of implementation. The low-level
implementation changed drastically between the Sun reference
implementations of versions 1.1 and 1.2, and this had no visible effect
on program behavior aside from improving performance. The JLS does
define a consistent behavior that was maintained despite a drastic
change.

We need to distinguish between problem domain and solution domain here.
Languages become better by moving the solution domain closer to the
problem domain. However, it's fundamental to problem domains that we
make distinctions between whether we're using the same entity or a
different one. You seem to have ambitions to raise programming
languages to such a high level that these problem-domain distinctions
are not representable in the solution space. I have to look at that as,
at the very least, counterproductive to solving real-world problems.

I think the whole situation would have been a lot clearer if the Java
developers had not tried to hide the fact that it is a heavily pointer
based language. Of course, it would have been necessary to explain that
Java pointers are much saner and safer than C pointers.

If what are called "references" in Java had been called "pointers", it
would have been obvious that all expressions and variables in Java are
either primitives or pointers, and both get passed by value.

Patricia
 
C

Chris Smith

George Cherry said:
class TestPassByValue3 {
private static void change(StringBuilder s) {
s.replace(0, 3, "New");
}

public static void main(String[] args) {
StringBuilder a = new StringBuilder("Old");
change(a);
System.out.println(a);
}
}

It's perhaps not a good idea to call this "TestPassByValue3", since the
result does not in ANY WAY depend on whether Java implements pass by
value or not. If you want to determine that, you need to use the lvalue
of the parameter. That's what I was trying to make clear all this time.
Your example is equivalent to the two examples of indirect values in my
original post, where the parameter passing mechanism is completely
irrelevant.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
G

George Cherry

Chris Smith said:
George Cherry said:
class TestPassByValue3 {
private static void change(StringBuilder s) {
s.replace(0, 3, "New");
}

public static void main(String[] args) {
StringBuilder a = new StringBuilder("Old");
change(a);
System.out.println(a); //Prints "New"
}
}

It's perhaps not a good idea to call this "TestPassByValue3", since the
result does not in ANY WAY depend on whether Java implements pass by
value or not. If you want to determine that, you need to use the lvalue
of the parameter. That's what I was trying to make clear all this time.
Your example is equivalent to the two examples of indirect values in my
original post, where the parameter passing mechanism is completely
irrelevant.

Okay, I see what you mean, but the purpose of
the above example was to show the op that passing
by value does NOT preclude the called method from
modifying an object on the heap referenced by
the parameter. Your first example

class TestPassByValue {
private static void change(String s) {
s = "New";
}

public static void main(String[] args) {
String a = "Old";
change(a);
System.out.println(a); //Prints "Old"
}
}

could give that misunderstanding. The reason
that your example is, I think, subject to confusing
the op about that is that you used a parameter
which referenced an immutable object. Yes,
your example does show that the assignment
to the formal by the called method does not
change the actual parameter in the caller's method,
but I think there is less confusing way to show this,
which follows.

The following example would have demonstrated
what you demonstrated without introducing the
possible confusion which I think your example could
cause.

class TestPassByValue4 {
private static void change(int i) {
i = i + 1;
System.out.println(i); //Prints 1
}

public static void main(String[] args) {
int i = 0;
change(i);
System.out.println(i); //Prints 0
}
}

Does what I just said make any pedagogical sense?

Boy, are we beating this subject to death, or what.

George W. Cherry
 
C

Chris Smith

George Cherry said:
could give that misunderstanding. The reason
that your example is, I think, subject to confusing
the op about that is that you used a parameter
which referenced an immutable object.

Okay, revised example:

class TestPassByValue
{
private static void change(Date dd)
{
dd = new Date();
}

public static void main(String[] args)
{
Date d = new Date(0L);
change(d);
System.out.println(d); // Jan 1, 1970
}
}

Your example was equivalent, except that the typical misunderstanding
goes something like "primitives are passed by value; objects are passed
by reference", so using a primitive doesn't end up demonstrating
anything either.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

Pete Barrett

I don't think that the distinction between objects and references really
does fall into the category of implementation. The low-level
implementation changed drastically between the Sun reference
implementations of versions 1.1 and 1.2, and this had no visible effect
on program behavior aside from improving performance. The JLS does
define a consistent behavior that was maintained despite a drastic
change.
Well, I think it does, but it's really just a matter of semantics.
What you call low level or implementation details, is a bit lower
level than what I call low level!
We need to distinguish between problem domain and solution domain here.
Languages become better by moving the solution domain closer to the
problem domain. However, it's fundamental to problem domains that we
make distinctions between whether we're using the same entity or a
different one. You seem to have ambitions to raise programming
languages to such a high level that these problem-domain distinctions
are not representable in the solution space. I have to look at that as,
at the very least, counterproductive to solving real-world problems.

I have no ambitions left, I assure you <g>. But it does seem to me
that over the past 30 years the tendency has been for practical
programming languages to more and more hide details such as these from
the programmer. I may be wrong, but it looks to me as if the designers
of Java would ideally like to have shielded the programmer from
needing to know that eg. creating an object sets aside space for it in
memory.

Pete Barrett
 
M

Mike Schilling

Patricia Shanahan said:
I think the whole situation would have been a lot clearer if the Java
developers had not tried to hide the fact that it is a heavily pointer
based language. Of course, it would have been necessary to explain that
Java pointers are much saner and safer than C pointers.

Yes, 100 times yes. In fact a pointer in Java is *exactly* the same as a
pointer in Pascal; it's how you access a dynamically created object.
 
G

George Cherry

Chris Smith said:
George Cherry said:
could give that misunderstanding. The reason
that your example is, I think, subject to confusing
the op about that is that you used a parameter
which referenced an immutable object.

Okay, revised example:

class TestPassByValue
{
private static void change(Date dd)
{
dd = new Date();
}

public static void main(String[] args)
{
Date d = new Date(0L);
change(d);
System.out.println(d); // Jan 1, 1970
}
}

Your example was equivalent, except that the typical misunderstanding
goes something like "primitives are passed by value; objects are passed
by reference", so using a primitive doesn't end up demonstrating
anything either.

I'm not sure that someone who doesn't know that
Java parameters are passed by value would know
what they need to know about Date to understand
the above example. So here I, George "Cavil", go
again. (BTW, I hid all the '{'s at the ends of lines. I
hope you can find them.) : o )

import java.util.Date;

class TestPassByValue {
private static void changeTime(Date dd) {
//new date, 365 days later than the epoch:
dd = new Date(31536000000L);
System.out.println(dd);//prints epoch + 365 days
}

public static void main(String[] args) {
Date d = new Date(0L); //sets date to the "epoch"
System.out.println(d); //prints the epoch
changeTime(d);
System.out.println(d); //still prints the epoch
}
}

George W. Cherry
 
D

Dale King

John said:
A very nice explanation, Dale. I feel compelled to raise one nitpick,
however (not even Java-related):


Fortran is in fact not restricted to any particular argument passing
mode. It specifies the semantics of subprogram calls, including effects
on subprogram arguments that are often implemented by compilers by means
of pass by reference, but that can also be implemented with value-result
(a.k.a copy in / copy out) in general, or with value or result
techniques in some specific cases. Fortran compilers exist, I believe,
that will choose among all these depending on the characteristics of the
subprogram involved. They are unquestionably free to do so.

No problem. As I said in the article the information mostly came from a
textbook and if what you said is correct then the book is wrong. I'll
check the book when I get back to work.
 
D

Dale King

Patricia said:
I think the whole situation would have been a lot clearer if the Java
developers had not tried to hide the fact that it is a heavily pointer
based language. Of course, it would have been necessary to explain that
Java pointers are much saner and safer than C pointers.

If what are called "references" in Java had been called "pointers", it
would have been obvious that all expressions and variables in Java are
either primitives or pointers, and both get passed by value.

But then they couldn't claim that there were no pointers. ;-) I'm sure
the marketing types had some responsibility there.

As I have said before there is no agreed upon definition on what
pointers are. It means different things to different people. Just see
any past discussion on whether Java has pointers. So they probably
thought that they were avoiding confusion by avoiding the word.
 
M

Mike Schilling

Dale King said:
But then they couldn't claim that there were no pointers. ;-) I'm sure the
marketing types had some responsibility there.

As I have said before there is no agreed upon definition on what pointers
are. It means different things to different people. Just see any past
discussion on whether Java has pointers. So they probably thought that
they were avoiding confusion by avoiding the word.


Q. Does Java have pointers?
A. No.

Q. Does Java have the same construct that Pascal calls pointers?
A. Look, a UFO!
 
D

Dale King

Mike said:
Q. Does Java have pointers?
A. No.

Q. Does Java have the same construct that Pascal calls pointers?
A. Look, a UFO!

The usual way I counter the arguments of whether Java has pointers is to
ask if Java has Frinkleheims. Can you answer that question if you don't
know what a Frinkleheim is. What relavence does your answer have if you
have a different definition than the person asking the question?
 
D

Dale King

Dale said:
No problem. As I said in the article the information mostly came from a
textbook and if what you said is correct then the book is wrong. I'll
check the book when I get back to work.

Checking the source again it says "FORTRAN only has reference mode;
expressions, which have no L-value, are evaluated and placed in
temporary locations in order to acquire an L-value for the duration of
the procedure." It also has a footnote that says some implementations of
FORTRAN store literals in a data region so that if you pass a literal
and change it within the function you may actually give the literal a
new value!

Perhaps the issue is with which variant of FORTRAN we are talking about.
I found one reference that said before 77, FORTRAN was pass by reference
and starting with 77 "scalar variables are often passed by value-result".

I have changed the text to say "Most FORTRAN implementations only have
reference mode."
 
G

George Cherry

Dale King said:
The usual way I counter the arguments of whether Java has pointers is to
ask if Java has Frinkleheims. Can you answer that question if you don't
know what a Frinkleheim is. What relavence does your answer have if you
have a different definition than the person asking the question?

Frinkleheims in Java 1.6?
 
B

blmblm

Checking the source again it says "FORTRAN only has reference mode;
expressions, which have no L-value, are evaluated and placed in
temporary locations in order to acquire an L-value for the duration of
the procedure." It also has a footnote that says some implementations of
FORTRAN store literals in a data region so that if you pass a literal
and change it within the function you may actually give the literal a
new value!

Perhaps the issue is with which variant of FORTRAN we are talking about.
I found one reference that said before 77, FORTRAN was pass by reference
and starting with 77 "scalar variables are often passed by value-result".

I have changed the text to say "Most FORTRAN implementations only have
reference mode."

This is probably okay if you want to keep things short. It seems a
little potentially misleading, since as far as I know compilers are
still free to do things such as passing by value-result, which I'm
not sure I'd describe as "reference mode". One of the reasons this
"works" is that Fortran (at least the FORTRAN 77 standard) disallows
passing identical/overlapping arguments to a subprogram, unless the
subprogram doesn't change any of them. I haven't been able to easily
find a current standard online, but I suspect this restriction has
not been lifted.

Also be advised that it's FORTRAN 77, but Fortran 90-and-later, AFAIK.
 
J

John C. Bollinger

Dale King wrote:

[...]
Checking the source again it says "FORTRAN only has reference mode;

I think the text is wrong, for any standardized version of Fortran, but
I am not sufficiently expert to say for sure. If you really want to
find out, the folks over in comp.lang.fortran are very friendly and very
knowledgeable. Some regulars have served (and may still do) on the ISO
Fortran standard committee, and at least one is the author of a popular
Fortran textbook.
expressions, which have no L-value, are evaluated and placed in
temporary locations in order to acquire an L-value for the duration of
the procedure." It also has a footnote that says some implementations of
FORTRAN store literals in a data region so that if you pass a literal
and change it within the function you may actually give the literal a
new value!

This is true, but you have to appreciate that such a program is not
standard-conforming, so whatever the implementation happens to do is
what you get.
Perhaps the issue is with which variant of FORTRAN we are talking about.
I found one reference that said before 77, FORTRAN was pass by reference
and starting with 77 "scalar variables are often passed by value-result".

I'm a bit out of my league at this point, but my understanding is that
no Fortran standard has ever required pass-by-reference semantics. The
comment you describe may be attempting to describe the characteristics
of Fortran implementations, but even at that I think it is probably
wrong in suggesting that no pre-F77 implementation used value-result,
and certainly wrong in suggesting that array variables are never passed
using value-result.
I have changed the text to say "Most FORTRAN implementations only have
reference mode."

That's probably a lot safer from nitpickers like me. :)
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top