2

When i passed an array as an argument to the function, the original array gets changed, but the array should not get changed right? please correct me if am wrong.

below i passed an int x=10 as an argument to change(int a) function. the value of the original int x got changed.

So how does the same code affects an array and int in different way?

public  class Runy  {

public static void main(String [] args) 
{
    Runy p = new Runy();
    p.start();
}

void start() 
{
    long [] a1 = {3,4,5};
    long [] a2 = fix(a1);

    int x=10;
    int y= change(x);

    System.out.println(y);
    System.out.println(x);

    System.out.print(a1[0] + a1[1] + a1[2] + " ");
    System.out.println(a2[0] + a2[1] + a2[2]);

}

long [] fix(long [] a3) 
{
    a3[1] = 7;
    return a3;
}
int change(int a)
{
    a=a+1;
    return a;
}

}

prabhakar Reddy G
  • 1,039
  • 3
  • 12
  • 23

2 Answers2

2

You're wrong. What you're passing in isn't the array - it's a reference to the array. Arrays are reference types in Java, so the value of a1 (for example) isn't an array object - it's a reference to an array object.

When you pass that reference into fix, the parameter (a3) has the same value as a1... that value refers to the same array, so a modification to that array is visible after the method returns. At that point, a1 and a2 will be equal references - they both refer to the same array.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • below i passed an int x=10 as an argument to change(int a) function. the value of the original int x got changed. So how does the same code affects an array and int in different way? – prabhakar Reddy G Oct 15 '15 at 04:51
  • @prabhakarReddyG: Because `int` is a value type, and arrays are reference types. With `int x = 10;` the value of `x` is just the integer 10... it's not a reference. – Jon Skeet Oct 15 '15 at 12:34
0

As Jon mentioned, you are passing a reference to the array and not a new array. If you don't want your old array to be modified, use Arrays.copyOf as seen below:

public class Runy {

    public static void main(String[] args) {
        Runy p = new Runy();
        p.start();
    }

    void start() {
        long[] a1 = {3, 4, 5};
        long[] a2 = fix(Arrays.copyOf(a1, a1.length));

        System.out.println(a1[0] + ", " + a1[1] + ", " + a1[2]);
        System.out.println(a2[0] + ", " + a2[1] + ", " + a2[2]);

    }

    long[] fix(long[] a3) {
        a3[1] = 7;
        return a3;
    }
}

You can also readup on System.arraycopy(). Here is a difference between both methods: System.arraycopy() vs. Arrays.copyOf() in Java

pelumi
  • 1,530
  • 12
  • 21
  • below i passed an int x=10 as an argument to change(int a) function. the value of the original int x got changed. So how does the same code affects an array and int in different way? – prabhakar Reddy G Oct 15 '15 at 04:51
  • Because arrays are `different` from ints. That said, once again arrays are passed by reference and primitive types like ins are passed by value. The meaning of passing an argument by reference is that you are sending the memory address of the data to the function, and changes within the function will be to the memory address which will be the same as the 'original array'. IF you don't want an array to get changed, you need to send a 'new' copy to the function. – pelumi Oct 15 '15 at 04:56
  • @pelumi: No, Java doesn't use pass by reference *at all*. The reference is passed by value, but that's *not* the same thing. – Jon Skeet Oct 15 '15 at 12:34