Results 1 to 5 of 5
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Question How to Pass byref in Java

    Dear All,

    How to Pass byref in Java like in C# codes below?

    Java Code:
    private boolean getPassByRef(int a, int b, ref int c) {
       boolean ret1 = false;
       int ret2 = 0;
       ret2 = (a * b) + 3;
       c = ret2;
       ret1 = true;
       return ret1;
    }

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Java copies everything by value.
    To "pass by reference" you will have to wrap primitives in an object, and pass a reference to it.

    You may want to look Q&A : How do I pass a primitive data type by reference?, its a fairly simple and clear explanation in my opinion (also the first result from google)

    Hope this helps,
    if you need a clearer explanation just ask

    Berkeleybross

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by newbiejava View Post
    Dear All,

    How to Pass byref in Java like in C# codes below? [ snip ]
    You can't; this question boils up ever so often: Java uses the pass by value mechanism only; remember that. You could have read that in any decent Java tutorial.

    kind regards,

    Jos

  4. #4
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Hi JosAh,

    so how to return multiple values? ..
    in my case below .. I have to return boolean and the math result?

    private boolean getPassByRef(int a, int b, ref int c) {
    boolean ret1 = false;
    int ret2 = 0;
    ret2 = (a * b) + 3;
    c = ret2;
    ret1 = true;
    return ret1;
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Pass String Value
    By DJCali in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 12:48 AM
  2. How to pass user-id between .java programs
    By mkkrish in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2009, 04:30 PM
  3. how to pass data
    By rakesh_mca in forum Java Servlet
    Replies: 7
    Last Post: 10-25-2009, 01:13 PM
  4. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 03:45 PM
  5. how to pass array in java?
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 01-13-2008, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •