Pass an integer by reference |
|
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;
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.