Re: no pointer in Java => my problem

From: Lasse Reichstein Nielsen (lrn_at_hotpop.com)
Date: 04/17/04


Date: Sat, 17 Apr 2004 15:38:57 +0200

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.
> 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
-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'


Relevant Pages

  • 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)
  • Re: The Java no pointer big fat lie!
    ... Does Java have auto variables that construct and destruct by scope? ... Are object references a native memory pointer or an abstracted ... > and reference) ...
    (comp.lang.java.programmer)
  • Re: pointer/ref question
    ... reference" to generically indicate we're conveying the address of an ... to /me/ one can describe both of these situations as "passing ... the term "pass by pointer" for the first case when teaching C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: pass by reference
    ... There really isn't a widely used definition of a reference. ... C++ means one thing; Java ... "pass pointer by value". ... grasps the difference between passing a pointer by value and passing ...
    (comp.lang.java.programmer)
  • Re: The Java no pointer big fat lie!
    ... > Whenever I read a Java book and it states that Java has no pointers, ... can't do pointer arithmetic, ie pointer+20 is impossible in Java. ... of the object they reference but to an intermediate which does that. ... A pointer is a memory address in C, no more, no less. ...
    (comp.lang.java.programmer)