0

i was working on stacks when i came across a problem where i had to double the array. i used this code which did not work but it should have.

class ArrayDouble
{
public static void main(String []args)
{
    int arr[] = new int[10];
    sizeChange(arr);
    for(int i=0;i<arr.length;i++)
        System.out.print(arr[i]+ " ");
}
public static void sizeChange(int arr[])
{
    arr[0]=1;
    arr = new int[2*arr.length];
    arr[1]=1;
}
}

from what i have seen, any changes that takes place in an array which has been passed as a parameter, the changes reflect back in the actual parameters right? So why doesn't the change in size of the array is reflected in the original parameter?

Also, arr[0] becomes 1 in the original array but arr[1] remains 0. why does that happen?

PS: the problem was solved when i changed the return type of sizeChange to int[] and passed arr of sizeChange and collected it into the Main arr. So i dont need the correction of the code, i just need the answer as to why this is happening.

Thank you in advanced.

Shivam Singh
  • 330
  • 2
  • 10
  • 1
    because Java passes a copy of the reference. So assigning a new instance to the parameter inside the method will not be reflected outside that. – davidxxx Sep 27 '19 at 16:04
  • Instead, return an array : `public static int arr[] sizeChange(int arr[]) { arr[0]=1; arr = new int[2*arr.length]; arr[1]=1; return arr; }` – davidxxx Sep 27 '19 at 16:05
  • okay so actual pass by reference is not taking place here? and since i have obviously changed the object and created a new object, so the changes are not taking place? is that it? – Shivam Singh Sep 27 '19 at 16:06
  • 1
    Java isn't pass by reference. It passes by value, but the values are references to objects. – Carcigenicate Sep 27 '19 at 16:08
  • @Carcigenicate (or primitives) – Michael Sep 27 '19 at 16:09
  • 1
    BTW: you have not changed the passed array (array size is not changeable), you have assigned a new array to the parameter. Hint: there is a `java.util.Arrays` class that has some methods like `copyOf()` that can be used for resizing an array (creating new array with same content) – user85421 Sep 27 '19 at 16:53

1 Answers1

1

Arrays are not a primitive type in Java, but they are not objects either ... "

In Java, the called method can update the contents of the array, and it can update its copy of the array reference, but it can't update the variable in the caller that holds the caller's array reference. Hence ... what Java is providing is NOT pass-by-reference.

Like all Java objects, arrays are passed by value ... but the value is the reference to the array. So, when you assign something to a cell of the array in the called method, you will be assigning to the same array object that the caller sees.

This is NOT pass-by-reference. Real pass-by-reference involves passing the address of a variable. With real pass-by-reference, the called method can assign to its local variable, and this causes the variable in the caller to be updated.

Detailed Explaination :
Arrays are in fact objects, so a reference is passed (the reference itself is passed by value, confused yet?). Quick example:

// assuming you allocated the list
public void addItem(Integer[] list, int item) {
    list[1] = item;
}

You will see the changes to the list from the calling code. However you can't change the reference itself, since it's passed by value:

// assuming you allocated the list
public void changeArray(Integer[] list) {
    list = null;
}

If you pass a non-null list, it won't be null by the time the method returns.

taurus05
  • 2,491
  • 15
  • 28
  • [Is an array an object in java](https://stackoverflow.com/questions/8781022/is-an-array-an-object-in-java) – Abra Sep 27 '19 at 16:10