Pass an integer by referenceTag(s): String/Number


Sometimes you may need to pass an integer to be able to change its value. "integer" are always passed by value in Java. An easy way to pass by reference is to use a single element array.
int[] a = new int[1];
a[0] = 1;
add2(a);
// a[0] now = 3
...

void add2(int[] a) {
    a[0] = a[0] + 2;
}

blog comments powered by Disqus