2

I'm trying to build a litle gam for android GSMs. I hava lots of units and all of them have destinations. To calculate destination I'm using a function called CalDes. This function CalDes is calculating my unit's speed. So I'm sending some variables into which I have to edit and use em again. For this I have to send these variables with reference. Here is a simple for C++ How to do that in java?

void swap(SomeType& arg1, Sometype& arg2) {
    SomeType temp = arg1;
    arg1 = arg2;
    arg2 = temp;
}
...
SomeType var1 = ...; // value "A"
SomeType var2 = ...; // value "B"
swap(var1, var2); // swaps their values!
Evren Ozturk
  • 809
  • 2
  • 19
  • 38
  • What do you mean there is no way to reference in Java? In Java every argument is passed by reference, except native types (int, long, byte, boolean, double and float). – Fred Feb 23 '12 at 11:17
  • 2
    Oh boy. Not another java "pass by value/pass by reference". Besides java is pass by value! – Umair Feb 23 '12 at 11:20
  • There are loads of existing questions on this topic, please search before posting. – DNA Feb 23 '12 at 11:30
  • @DNA: I'm not seeing a good duplicate for this specific question. Lots of questions about whether Java has pass-by-reference, but not about how you do what the OP wants. The closest I see [is this one](http://stackoverflow.com/questions/2762171/when-you-need-pass-by-reference-in-java-to-assign-values-to-multiple-parameters), but the water is rather muddied by the OP's goals in that case. – T.J. Crowder Feb 23 '12 at 11:40
  • Fair point - this one might be useful though: http://stackoverflow.com/questions/3624525/how-to-write-a-basic-swap-function-in-java – DNA Feb 23 '12 at 11:43
  • @DNA: LOL Okay, this is definitely a duplication of **that**. :-) – T.J. Crowder Feb 23 '12 at 11:57
  • possible duplicate of [Java: How to pass byte\[\] by reference?](http://stackoverflow.com/questions/333151/java-how-to-pass-byte-by-reference) – Mechanical snail Aug 06 '12 at 13:01
  • possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Pere Villega Aug 06 '12 at 18:43

3 Answers3

14

Java doesn't have pass-by-reference at all. You have a couple of options:

  1. Pass the data in an array:

    void swap(SomeType[] args) {
        SomeType temp = args[0];
        args[0] = args[1];
        args[1] = temp;
    }
    

    ...but using it is a pain in the calling code:

    SomeType a = ...;
    SomeType b = ...;
    SomeType[] args = new SomeType[] { a, b };
    swap(args);
    a = args[0];
    b = args[1];
    
  2. Create an object that has public SomeType members, and pass in the object reference.

    void swap(SomeTypeContainer c) {
        SomeType temp = c.a;
        c.a = c.b;
        c.b = temp;
    }
    

    Which is very similar to #1, but probably more convenient to work with from calling code.

    SomeTypeContainer c = new SomeTypeContainer(/* ... something that creates a and b ... */);
    // Use c.a and c.b directly
    swap(c);
    // Use c.a and c.b directly, they're now swapped
    
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
2

Not possible in Java, read: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Pointer Null
  • 36,993
  • 13
  • 79
  • 106
2

In Java, every object is always passed by reference. But you cannot pass variables by reference. If you really need that, you have to use one more indirection.

jmg
  • 6,817
  • 1
  • 16
  • 21
  • 1
    Not exactly - see http://stackoverflow.com/questions/40480/is-java-pass-by-reference – DNA Feb 23 '12 at 11:22
  • 1
    @DNA: Well, this is --- as many things are --- a question of perspective. If we talk about references, then Java is pass by value. If we talk about objects, it is pass by reference. Java's approach to passing is different from most other predating languages. You can't decide how to pass a value or object. It is impossible to pass an object by value in Java. And you can't pass a primitive type by reference in Java. That is why there are type like java.lang.Integer, et. al. And you can't pass a reference to a reference. That is what the OP asks for. – jmg Feb 23 '12 at 11:31
  • 1
    @jmg: The key here is that "pass-by-reference" is a specific term: It means passing a reference *to the **variable** in the calling context* into the called context, so the called context can modify the calling context's variable's content. Absolutely nothing whatsoever to do with object references. Java is entirely pass-by-value. The **value** being passed, in the case of objects, is an object reference, but that has nothing whatsoever to do with pass-by-reference. – T.J. Crowder Feb 23 '12 at 11:34
  • 1
    See also http://javadude.com/articles/passbyvalue.htm - which says it better than I can - "Objects are not passed by reference. A correct statement would be Object references are passed by value.This may seem like splitting hairs, bit it is far from it. There is a world of difference in meaning..." – DNA Feb 23 '12 at 11:38