Pass by reference in java?

19,324

Solution 1

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
    

Solution 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.

Solution 3

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

Share:
19,324
Evren Ozturk
Author by

Evren Ozturk

I'm a programmer, a 3D modeller, photoshop user, a painter & an old Flash animator. Each of them is a world that you are the creator & that means you can create, delete or edit things as you wish! :) Programming is my favorite, especially game programming. The only thing I want is to learn much more about the programming world. As they say Life runs on code.

Updated on June 14, 2022

Comments

  • Evren Ozturk
    Evren Ozturk almost 2 years

    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!
    
    • Fred
      Fred about 12 years
      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).
    • Umair
      Umair about 12 years
      Oh boy. Not another java "pass by value/pass by reference". Besides java is pass by value!
    • DNA
      DNA about 12 years
      There are loads of existing questions on this topic, please search before posting.
    • T.J. Crowder
      T.J. Crowder about 12 years
      @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, but the water is rather muddied by the OP's goals in that case.
    • DNA
      DNA about 12 years
      Fair point - this one might be useful though: stackoverflow.com/questions/3624525/…
    • T.J. Crowder
      T.J. Crowder about 12 years
      @DNA: LOL Okay, this is definitely a duplication of that. :-)
    • Mechanical snail
      Mechanical snail over 11 years
    • Pere Villega
      Pere Villega over 11 years
      possible duplicate of Is Java "pass-by-reference"?
  • DNA
    DNA about 12 years
  • jmg
    jmg about 12 years
    @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.
  • T.J. Crowder
    T.J. Crowder about 12 years
    @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.
  • DNA
    DNA about 12 years
    See also 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..."
  • Evren Ozturk
    Evren Ozturk about 12 years
    Thank you for great explanation.