Pass an integer by reference using array in Java
Description
The following code shows how to pass an integer by reference using array.
Example
/*w ww.j a v a2 s . com*/
public class Main {
public static void main(String[] argv) {
int[] a = new int[1];
a[0] = 1;
add(a);
System.out.println(a[0]);
}
static void add(int[] a) {
a[0] = a[0] + 2;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »