3

I was wondering, in java, is it possible to in anyway, simulate pass by reference for an array? Yes, I know the language doesn't support it, but is there anyway I can do it. Say, for example, I want to create a method that reverses the order of all the elements in an array. (I know that this code snippet isn't the best example, as there is a better algorithms to do this, but this is a good example of the type of thing I want to do for more complex problems).

Currently, I need to make a class like this:

public static void reverse(Object[] arr) {
    Object[] tmpArr = new Object[arr.length];
    count = arr.length - 1;
    for(Object i : arr)
        tmpArr[count--] = i;
    // I would like to do arr = tmpArr, but that will only make the shallow
    // reference tmpArr, I would like to actually change the pointer they passed in
    // Not just the values in the array, so I have to do this:
    for(Object i : tmpArr)
        arr[count++] = i;
    return;
}

Yes, I know that I could just swap the values until I get to the middle, and it would be much more efficient, but for other, more complex purposes, is there anyway that I can manipulate the actual pointer?

Again, thank you.

Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
  • 1
    An array extends from java.lang.Object. Objects in Java are always passed by reference. – Dan Mar 19 '10 at 03:31
  • 1
    @Dan: NO! _References to_ objects in Java are _passed by value_. And yes, that means something entirely different. – polygenelubricants Mar 19 '10 at 03:46
  • 1
    This explains it well: http://stackoverflow.com/questions/40480/is-java-pass-by-reference/40523#40523 "Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value." – Jim Ferrans Mar 19 '10 at 04:02
  • @polygenelubricants: how can I tell the difference between a value passed by reference and a reference passed by value? – Gabe Mar 19 '10 at 04:34
  • In Java, there is no need to differentiate between either mechanism since one isn't even possible; everything is passed by value. I recommend some reading if you want to really understand what each definition really means. – polygenelubricants Mar 19 '10 at 04:49

5 Answers5

2

is there anyway that I can manipulate the actual pointer?

Java does not pass by reference, so you can't directly manipulate the original pointer. As you've found out, Java passes everything by value. You can't pass a reference to an array object, and expect a method to modify the original reference to point to another array object.

You can, of course:

  • Modify elements of the referred array object (ala java.util.Arrays.sort)
  • Pass a reference to an object with a settable field (e.g. Throwable has a setStackTrace)
  • return the new reference instead (ala java.util.Arrays.copyOf)
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • Returning the new reference only works if you have a single array you're working with. – Gabe Mar 19 '10 at 04:33
  • You can return an array of arrays, or encapsulate multiple arrays as a new type. – polygenelubricants Mar 19 '10 at 04:38
  • The only problem with that would be that it would be awkward for the programmer to have to put the array in another array to call it, but I guess that would work for other, more appropriate situations. – Leif Andersen Mar 19 '10 at 12:12
1

Well, you can explicitly pass an object that contains a reference. java.util.concurrent.atomic.AtomicReference is ready out of the box, although it does come with volatile semantics that you probably don't want. Some people use single element arrays to returns values from anonymous inner classes (although that doesn't seem a great idea to me).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
1

This method reverses the Array's elements in place. The caller sees the changes. (In Java everything is passed by value, including object references.)

   public static void reverse(Object[] arr) {
       for ( int i = 0, j = arr.length - 1;   i < j;   i++, j-- ) {
           Object temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
       }
   }
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
0

In Java Object reference is passed by value.

So if you looking for something like

function referenceCheck()
{
    int[] array = new int[]{10, 20, 30};
    reassignArray(&array);
    //Now array should contain 1,2,3,4,5
}

function reassignArray(int **array)
{
    int *array = new int[] { 1, 2, 3, 4, 5};
}

Then its not possible in Java by any direct means.

If we need to change only the values stored in an array, then we can do it since object reference is passed by value.

Thiyaneshwaran S
  • 1,137
  • 2
  • 10
  • 14
-1

You want to pass a reference to the array reference. In that case you just have to either create a class to hold the reference and pass a reference to that class or just pass a 1-element array of the type being passed. Then you'd be passing either an object holding the array or an array whose only element contains the array you want to operate on.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Yes, I have tried it, and I know that java doesn't create a whole array. Rather, it passes in a reference, or pointer, to the array. However, it doesn't actually pass in the reference used when the programmer calls the method, but rather creates a new one. So, when you would say 'arr = tmpArr', arr does begin to point to tmpArr, but when it leaves that method, arr, and whatever the programmer passed in for arr aren't the same, meaning that they get the old, unrevised, array. – Leif Andersen Mar 19 '10 at 03:29