Do we need "pass by reference" in Java?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

View Poll Results: Is Network Programming a demanded speciality?

Voters
1. You may not vote on this poll
  • Its a growing speciality

    0 0%
  • Yes

    1 100.00%
  • No

    0 0%
  • It's almost disappeared

    0 0%
  • I'm not familiar with it

    0 0%
Results 1 to 7 of 7

Thread: Do we need "pass by reference" in Java?

  1. #1
    Join Date
    Mar 2003
    Posts
    7

    Do we need "pass by reference" in Java?

    Currently the Java language allows to pass method parameters only by value. This means that if one wants to change a parameter in the method and keep the changed value outside the method there is no direct way to do it. For example if you want to write a method to swap values you cannot do it like that:

    public void swap(int a, int b) {
    int t = a;
    a = b;
    b = t;
    }

    you have to do it like that:

    public void swap(int[] a, int[] b) {
    int t = a[0];
    a[0] = b[0];
    b[0] = t;
    }

    Do you think including pass by reference construct similar to C++ "&", Pascal's "var" or C#'s "ref" is a good idea?

  2. #2
    Join Date
    Nov 2002
    Posts
    59
    I'm afraid your information is a little bit off...java passes all objects by references, and all simple values by copy...for instance
    int method(Object a) {
    }
    passes the object by reference, not value. So to pass an integer or the likes by reference, you simply need to wrap it as an object like Integer(int)

    So in effect, I don't think that they should change anything...while pointers can be powerful at times, it would somewhat defeat the java runtime

  3. #3
    Join Date
    Mar 2003
    Posts
    834
    "I'm afraid your information is a little bit off"....no it isn't, it's just quite subtle.

    All passing is Java is PASS BY VALUE. No exceptions. If you do Java Programmer Certification, or look into any books on the subject, you will find that sentence.

    You don't actually "pass the object by reference". You can never pass an object, you can only pass REFERENCES to objects and the value of references passed to methods are COPIED i.e. PASS BY VALUE.
    ArchAngel.
    O:-)

  4. #4
    Join Date
    Nov 2002
    Posts
    59
    Ah, I see your point. Thanks for the clarification.
    But in effect, pointers in C++ are pass by value too...they pass the value of the memory address. So how is it different than Java passing the value of the reference to an object? Just confused. Thanks.

  5. #5
    Join Date
    Mar 2003
    Posts
    7

    in C++ you can do pointers to pointers to .... :)

    But in C/C++ you can pass a pointer to the pointer

    The interesting thing is that despite the pointer operators in C they desided to add the reference operator in C++ just to make it a little bit more convenient and I think they did right!

    In C you can do it like that:

    void swap(int * a, int * b) {
    int t = *a;
    *a = *b;
    *b = t;
    }

    then you call:
    int a = 2;
    int b = 3;
    swap(&a, &b);

    But in order to save you the constant referencing/dereferencing in C++ they added the & operator, and it looks like that:

    void swap(int & a, int & b) {
    int t = a;
    a = b;
    b = t;
    }

    and you call it like that:

    int a = 2;
    int b = 3;
    swap(a, b);

    Cleaner, heh :?)

  6. #6
    Join Date
    Nov 2002
    Posts
    59
    Yep, but how is that still different than
    public int method(Object a) {
    }
    public void main(String argv[]) {
    method(new Object(...))
    }

    Anyway, you get the point...when I pass the method, and I change any of the values, it's changed in the original memory...so isn't that the same as passing a reference in C++?

  7. #7
    Join Date
    Mar 2003
    Posts
    7

    returning multiple values?

    If you want to return multiple values you have no way but to use a wrapper object.

    How are you going to do that now:
    (1)
    public void createMultiple(Object a, Object b) {
    a = new Object();
    b = new Object();
    }


    Object a = null;
    Object b = null;

    createMultiple(a, b);

    // a and b are still null after the method call

    In order for this to work you have to create a wrapper
    (2)
    public class Wrapper {
    public Object a;
    public Object b;
    }

    public void createMultiple(Wrapper w) {
    w.a = new Object();
    w.b = new Object();
    }

    then this will work:

    Wrapper w = new Wrapper();
    createMultiple(w);

    Object a = w.a;
    Object b = w.b;

    If we have references we do just that

    public void createMultiple(Object & a, Object & b) {
    a = new Object();
    b = new Object();
    }

    this way the first snippet (1) will work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center