Re: no pointer in Java => my problem
From: Lasse Reichstein Nielsen (lrn_at_hotpop.com)
Date: 04/17/04
- Next message: Enrique: "Re: table first"
- Previous message: GaryM: "Day boundary comparisons"
- In reply to: Bryan Castillo: "Re: no pointer in Java => my problem"
- Next in thread: Bryan Castillo: "Re: no pointer in Java => my problem"
- Reply: Bryan Castillo: "Re: no pointer in Java => my problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.'
- Next message: Enrique: "Re: table first"
- Previous message: GaryM: "Day boundary comparisons"
- In reply to: Bryan Castillo: "Re: no pointer in Java => my problem"
- Next in thread: Bryan Castillo: "Re: no pointer in Java => my problem"
- Reply: Bryan Castillo: "Re: no pointer in Java => my problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|