6
public class JavaApplication6 {

    public static void a(int b)
    {
        b++;
    }

I am calling the function a and passing the variable b with the intention of incrementing it like a C++ reference (&b). Will this work? If not, why?

    public static void main(String[] args) {

        int b=0;
        a(b);
        System.out.println(b);
    }

}
Charles
  • 1,384
  • 11
  • 18
striker
  • 77
  • 8
  • 4
    You can't do that. – SLaks Oct 10 '16 at 17:28
  • 1
    but why is that i can do it in c++ by just passing the variable to function by reference is there any way of doing it except global declaration – striker Oct 10 '16 at 17:29
  • 2
    In Java it's pass by value and hence you cant change the value of b. Whereas in programming language like C, C++, you can pass pointer and using pointer you can increment the value. – SMA Oct 10 '16 at 17:29
  • please tell me any alternative i can use – striker Oct 10 '16 at 17:30
  • still no gain it is not working "java.lang.Integer" – striker Oct 10 '16 at 17:31
  • 2
    This is generally a bad idea, and leads to complex unreadable code. What problem are you trying to solve? – SLaks Oct 10 '16 at 17:34
  • 1
    Java is strictly pass-by-value without exception, so this isn't possible. – QBrute Oct 10 '16 at 17:35
  • Java is _only_ pass by value/copy. For more, see [this link](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Charles Oct 10 '16 at 17:36
  • 2
    You can't "natively" do this, but you could define your own `MyInt` class with a single reference of a member variable `int b` that you can modify – OneCricketeer Oct 10 '16 at 17:36
  • Yeah like many others have said Java is designed to have _no_ pass-by-reference – sova Oct 10 '16 at 17:41
  • @cricket_007 There is already a java class that does this called AtomicInteger. See my answer for more details. – nasukkin Oct 10 '16 at 17:43
  • http://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java – Flakkenmarsh Oct 11 '16 at 09:50

4 Answers4

3

While you can't really accomplish that with int (the primitive type for integer), you can accomplish something very similar with AtomicInteger. Simply invoke the getAndIncrement method on an instance of the class. Something like this:

public static void a(AtomicInteger b) {
    b.getAndIncrement();
}

(Note that you also can't do this with java.lang.Integer, because java.lang.Integer is an immutable class.)

nasukkin
  • 2,460
  • 1
  • 12
  • 19
  • 1
    You can do this for any data type with an `AtomicReference`. It's far from idiomatic Java, though, and is generally discouraged. – Andrew Rueckert Oct 10 '16 at 18:21
  • 1
    While this accomplishes the task, using `AtomicInteger` just to use its mutable property is probably not the best idea because of the non-blocking sync overhead. – Patrick Oct 10 '16 at 18:29
3

First of all: Java does not allow for pass-by-reference. Further, out-parameters (when a function's calculations/results are placed in one or more of the variables passed to it) are not used; instead, something is returned from a method like so:

b = a(b);

Otherwise, in Java, you pass objects as pointers (which are incorrectly called references). Unfortunately (in your case) most types corresponding to int (Integer, BigInteger, etc.) are immutable, so you cannot change the properties in the object without creating a new one. You can, however, make your own implementation:

public static class MutableInteger {
    public int value;

    public MutableInteger(int value) {
        this.value = value;
    }
}

public static void main(String[] args) {
    MutableInteger b = new MutableInteger(2);
    increment(b);
    System.out.println(b.value);
}

public static void increment(MutableInteger mutableInteger) {
    mutableInteger.value++;
}

The following will be printed to the console when this code is run:

3

At the end of the day, using the above requires a strong argument on the programmer's part.

Charles
  • 1,384
  • 11
  • 18
Patrick
  • 33,984
  • 10
  • 106
  • 126
1

You can't do that with primitive types like int, because Java passes primitives by value. Simply wrap b in a mutable container object :

class Holder {
  int b;
}

Now you can modify the value by doing :

public static void a(Holder h) {
  h.b++;
}

You should, however, consider to work with Immutable Objects, which are generally considered a good thing in Java to avoid side effects and concurrency issues.

Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
0

You cannot pass a value by reference in Java. If a primitive is passed to a method, the JVM passes it by value. If it is an object that is sent to a method, the JVM creates a copy of the its reference. Since this is a "copy" the modification won't change the initial object. But still, there are workaround such as using the AtomicInteger suggested in the previous posts.

Joseph M. Dion
  • 356
  • 3
  • 7