Re: no pointer in Java => my problem

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

  • Next message: Liz: "Re: Advice regarding 1099 consulting position"
    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.


  • Next message: Liz: "Re: Advice regarding 1099 consulting position"

    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: Question on LSP
      ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
      (comp.object)
    • Re: ByVal vs. ByRef
      ... But I still don't understand why I have to pass the TreeNode argument ByVal. ... I understand why passing it ByVal works. ... What is "the Value" of a reference variable? ... A "pointer" to the object. ...
      (microsoft.public.dotnet.languages.vb)
    • 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)