Re: no pointer in Java => my problem

From: Bryan Castillo (rook_5150_at_yahoo.com)
Date: 04/19/04


Date: 19 Apr 2004 11:30:07 -0700

Lasse Reichstein Nielsen <lrn@hotpop.com> wrote in message news:<vfjy67wu.fsf@hotpop.com>...
> rook_5150@yahoo.com (Bryan Castillo) writes:
>
> > 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?
>
> Of C, C++ and Java, yes. There is a slight, semantic, difference
> between passing a pointer to a value and passing a reference to a
> variable. Sure you can implement the latter with the former, but
> not the other way around. When passing a reference, you don't get
> access to the pointer.
>
> > 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.
>
> What the implementation does is not the point. It's probably machine
> code anyway, where there are no pointers or references, just bit
> patterns. The difference is what it looks like in the language. In C++
> you can write
>
> void foo(int &x) {
> x++;
> }
> ...
> int y = 24;
> foo(y);
>
> Inside foo, x is just another variable. Outside foo, y is just another
> variable. It is the way the paramter is passed that makes the difference.
>
> Neither Java nor C have anything resembling this. C can pass pointers,
> but then the pointer becomes the value. You need to explicitly
> dereference it to affect the memory cell it is pointing to. Java
> has nothing.
>
> Notice that both C++ and Java have mutable structures that can be
> passed as values and changed (C actually passes struct's as values).
> That is not the same as a reference parameter.
>
> > (I don't see how there is really any difference, except the
> > language semantics).
>
> That *is* the point. It makes a difference in what you can do, how
> easy you can do it, and what the compiler can assume. You can do better
> optimizations when you know that references to variables can't be
> passed around. Java is designed to be safe, something you can never
> be with first class pointers. Take this C program:
> ---
> void foo() {
> int i=42;
> return &i;
> }
>
> void bar(int *p) {
> int i;
> printf("%d - %d\n",*p,i);
> }
>
> int main(int argc,char *argv[]) {
> bar(foo());
> }
> ---
> > Is that pass be reference then - so C does have pass by reference?
>
> No, that is an encoding that works like it. You can also encode objects
> in C, but that doesn't make it object oriented.

It doesn't? Sure the language may not be object oriented, however I
can design applications using objects and still implement them in C.
I have done this many times using structs containing pointers to
functions, so the functions used to implement the behavior of the
objects can be changed.

I guess you, being a language theroretician, would care how the
language supports the design and I care about it in terms of
maintaining applications over time, however the design of the
application always matters to me more.

Regarding the concepts of addresses, references and pointers, I see it
as only 2 options. 1 - there are ways to pass things around to other
control constructs (functions, objects, whatever...) where the thing
is logically changed to the creator or originator of the thing. 2 -
there are ways to pass things around to other control structures
where the changes to the thing are not seen by the orginator.

To me, passing by address or reference usually falls under case 1
while passing by value falls under case 2.

In java, passing around objects which may be modified falls under case
1 and primitive types fall under case 2. You might go farther and
start talking about objects returned through RMI or SOAP (RPC) and
start classifying those objects as being modifiable and visible to
another scope or not.

It just seems to me that describing parameters to methods as being
passed by reference, address or value, does not accurately describe or
address the issues you face while designing applications.

>
> > Why do people even argue about such a useless topic (pass by value or
> > pass by reference)? It shouldn't really matter.
>
> Well, I *am* a language theroretician, so I guess because it's fun :)
> /L



Relevant Pages

  • Re: pointer syntax
    ... > really passing a reference (which is like an old-school pointer, ... > a copy of the primitive's value and passing that. ... build them yourself with pointers. ... mean a var parameter, write a var parameter. ...
    (comp.lang.pascal.delphi.misc)
  • Re: programming concepts > specific languages
    ... > pointers if you are just doing java all along. ... Java has consistent, but non-intuitive, rules involving passing by ... value/passing by reference. ... A lot of beginning Java programmers don't ...
    (comp.programming)
  • Re: pointers and array of pointers
    ... I often think of pointers as a VARIABLE that contains a reference ... Want you want to do in myswap is pass the ... You do this by passing ...
    (comp.lang.c)
  • Re: Passing by reference
    ... It depends upon the reason one is passing the reference. ... instantiate all relationships via a constructor so there will always be ... I naturally think of 'getter' methods as 'knowledge ...
    (comp.object)
  • Re: Using ref
    ... the address of a variable that contains a reference to an object, ... the C version is passing the address of a variable that contains the   ... Inasmuch as the specification is unambiguous, there is a single truth. ... Every time he states the incorrect view, I will feel compelled to correct his incorrect statements. ...
    (microsoft.public.dotnet.languages.csharp)