pass by reference an integer in java

Viewed 1085

I need to pass an integer by reference in java . Is there a simple way to do so ? In C++ by putting "&" before an integer would be passed by reference . This is the C code that I'm trying to turn to Java :

void count(int distance, int i, int &counter, int array[], int n) {
    if (i == distance) 
        counter++;
    else {
        for (int j = 0; j < n; j++) {
            if (i <= distance - array[j]) 
                count(distance, i + array[j], counter, array, n);
        }
    }
}

Is there a way to do so without having an integer object ?? (I don't want to make another class )

7 Answers

You will need an object, but you don't have to build it out yourself. As Andy Turner said, you could use either an int array or the AtomicInteger so:

int[] counter = new int[]{0};
counter[0]++;

..

AtomicInteger counter = new AtomicInteger();
counter.incrementAndGet();

OR

You can use MutableInt in the commons-lang package

MutableInt counter = new MutableInt();
counter.increment();

You can't pass by reference in Java.

You can either pass in a mutable container, for example an int[1] or an AtomicInteger:

void count(int distance, int i, int[] counter, int array[], int n)

Or you could use a return value to return the updated value of counter:

int count(int distance, int i, int array[], int n) {
  if (i == distance) return 1;
  else {
      int counter = 0;
      for (int j = 0; j < n; j++) {
          if (i <= distance - array[j]) counter += count(distance, i + array[j], array, n);
      }
      return counter;
  }
}

You might need to have your method return counter

I am not sure this is the same algorithm but this is an illustration of what I have in mind:

public static void main(String[] args) {
    int counter = 3;
    counter = count(2, 1, counter, new int[] {1,2,3}, 3);
    System.out.println(counter);
}

static int count(int distance, int i, int counter, int array[], int n) {
    if (i == distance) {
        counter++;
    } else {
        for (int j = 0; j < n; j++) {
            if (i <= distance - array[j])
                counter = count(distance, i + array[j], counter, array, n);
        }
    }
    return counter;
}

The only way to pass a primitive type by reference in Java is to wrap it in an object. Fundamentally, you cannot pass primitive types by reference because they are not object-oriented.

Check out this post for more information: How do I pass a primitive data type by reference?

In java primitive variables are always bind with 'pass by value'. If you want to achieve pass by reference functionality you will need to pass those values with the help of objects. You can have a look at the examples at this link:- Call by Value and Call by Reference in Java

As you know Java is Pass by Value for Primitive data type (even for Wrapper like Integer) One Way - Pass a class object having i as instance member. Another way - make i as instance member of class and do not pass i as method parameter.

A different (functional) solution, pass a Runnable that does the counting (or whatever is required):

void count(int distance, int i, Runnable counter, int array[], int n) {
    if (i == distance) {
        counter.run();
    }

which would be called like in:

private int count;  // does not work with local variable
    // ...
    count(distance, i, ()->count++, array, n);

or, using a method:

    count(distance, i, this::increment, array, n);
    // ...
private void increment() {
    count++;
}
Related