Post Reply 
 
Thread Rating:
  • 1 Votes - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass an integer by reference
12-14-2010, 08:29 AM
Post: #1
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;
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved