Re: no pointer in Java => my problem
From: Bryan Castillo (rook_5150_at_yahoo.com)
Date: 04/17/04
- Previous message: Roedy Green: "Re: Simple newby program, multiple problems - help please!"
- Maybe in reply to: marcus: "Re: no pointer in Java => my problem"
- Next in thread: Lasse Reichstein Nielsen: "Re: no pointer in Java => my problem"
- Reply: Lasse Reichstein Nielsen: "Re: no pointer in Java => my problem"
- Reply: Daniel Sjöblom: "Re: no pointer in Java => my problem"
- Reply: Roedy Green: "Re: no pointer in Java => my problem"
- Reply: Dale King: "Re: no pointer in Java => my problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 16 Apr 2004 17:31:07 -0700
Daniel Sjöblom <dsjoblom@mbnet.fi_NOSPAM> wrote in message news:<407ef28b$0$21236$7b6a8dc4@news.mbnet.fi>...
> rowla wrote:
> > I have a class:
> > class Z {
> > int i;
> > public Z(int y) {this.i=y;}
> > }
> >
> > and a function
> > private void f(Z j) {
> > Z s = new Z(65);
> > j=s;
> > }
> >
> > when I do:
> > Z j = new Z(47);
> > f(j);
> > System.out.println(j.i);
> >
> > I get 47 from System.out.println(j.i);
> > what should I do to get 65?
>
> Your problem is not related to pointers.
>
> void f(Z j) { j.i = 65; }
>
> There are no value objects in java. Z is a reference passed by value.
> References are similar to C pointers, except no pointer arithmetic (the
> . operator dereferences, similar to -> in C). To simulate C style:
>
> void f(struct Object **o) { *o = malloc(sizeof(struct Object)); }
>
> you wrap your Object in a wrapper in java:
>
> class Wrapper
> {
> Object o;
> }
>
> void f(Wrapper w) { w.o = new Object(); }
>
> Java and C are both pass by value only. You can't change the value of a
> passed pointer in a C function either (that is, it won't affect the
> pointer from the view of the caller).
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? 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.
Ultimately you are probably just copying some 32bits into a register
and modifying that won't affect the caller. To affect the caller you
are modifying a value through another value, address, reference or
object (I don't see how there is really any difference, except the
language semantics). In c I could create a macro to make it like im
passing by reference.
#include <stdio.h>
#define pass_by_ref(A) _call_it(&(A))
void _call_it(int * a) {
*a = 0;
}
int main (void) {
int value;
value = 10;
printf("value = %d\n", value);
pass_by_ref(value);
printf("value = %d\n", value);
return 0;
}
Is that pass be reference then - so C does have pass by reference?
Why do people even argue about such a useless topic (pass by value or
pass by reference)? It shouldn't really matter.
>
> Anyhow, your design is probably broken. Pass by reference semantics (as
> in, behaves as if passed by reference) are rarely needed.
- Previous message: Roedy Green: "Re: Simple newby program, multiple problems - help please!"
- Maybe in reply to: marcus: "Re: no pointer in Java => my problem"
- Next in thread: Lasse Reichstein Nielsen: "Re: no pointer in Java => my problem"
- Reply: Lasse Reichstein Nielsen: "Re: no pointer in Java => my problem"
- Reply: Daniel Sjöblom: "Re: no pointer in Java => my problem"
- Reply: Roedy Green: "Re: no pointer in Java => my problem"
- Reply: Dale King: "Re: no pointer in Java => my problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|