Copying address of one array into another

37 views Asked by At
public class zeroCheck {
    
    int a[]={2,3,4};
    int c[]={5,6,7};

    void addMore(int arr[])
    {
        int another[] = new int[6];
        System.arraycopy(a, 0, another, 0, 3);
        System.arraycopy(c, 0, another, 3, 3);
         
        for(int x:another)
        {
            System.out.print(x + "\t");
        }
        // prints 2 3 4 5 6 7 0

        // Trying to copy address of another array into the original
        arr = another;

    }

    public static void main(String args[])
    {
        zeroCheck o=new zeroCheck();
        // System.out.println(o.a.length);

        int b[] = new int[6];
        o.addMore(b);
            
        for(int x:b)
        {
            System.out.println(x);
        }
        // prints 0 0 0 0 0 
        
    }
    
}

Here I was trying to copy the address of another into the parameter, but it looks like it didn't affect the original array. I speculate an arrayname contains the address, so it should be pass by reference meaning any change in the addMore method will reflect in the main. So, the array b[] should be the same as another[].

I ran the code and the output appears to be like this:

2 3 4 5 6 7 0
0
0
0
0
0

What does this mean?

0

There are 0 answers