5

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 )

Bentaye
  • 9,403
  • 5
  • 32
  • 45
vanes
  • 83
  • 8
  • 4
    You could return *counter*, i.e. int instead of void – Bentaye Feb 27 '20 at 13:37
  • An integer is not an object, my understanding is that only objects can be passed by reference. Try wrapping it up in an object and then pass that by reference. https://www.journaldev.com/3884/java-is-pass-by-value-and-not-pass-by-reference – SPlatten Feb 27 '20 at 13:38
  • 2
    @SPlatten Java has Integer and int (integer). The first one is an object of primitive type int. – Reporter Feb 27 '20 at 13:39
  • You can't pass anything by reference in Java, but you can pass a mutable object. – molbdnilo Feb 27 '20 at 13:39
  • @reporter, thank you, hence my suggestion. – SPlatten Feb 27 '20 at 13:40
  • 1
    Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Amongalen Feb 27 '20 at 13:40
  • @Bentaye I didn't get the answer I needed when I did thatbut thanks :) – vanes Feb 27 '20 at 13:44
  • @Amongalen I've already read it and know the difference but thanks :) – vanes Feb 27 '20 at 13:45
  • You could pass an `int[]` with one value instead of an `int`. You'd probably be better off using a return value though. – khelwood Feb 27 '20 at 13:47
  • Integer is an object, but it is immutable, so you'll need yet another object. I'd return it as a value, though. – NomadMaker Feb 27 '20 at 13:47
  • @vanessa If you've understood that you can't pass by reference in java you wouldn't ask if it is possible. – Amongalen Feb 27 '20 at 13:48
  • 1
    Does this answer your question? [How to do the equivalent of pass by reference for primitives in Java](https://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java) – Sharad Nanda Feb 28 '20 at 09:32

7 Answers7

4

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();
Neeraj
  • 2,376
  • 2
  • 24
  • 41
2

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;
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
2

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;
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
1

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?

Jaeheon Shim
  • 491
  • 5
  • 14
0

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

Sharad Nanda
  • 406
  • 1
  • 15
  • 27
0

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.

Mak21k
  • 11
  • 1
0

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++;
}
user85421
  • 28,957
  • 10
  • 64
  • 87