Java-Array as Parameters of Method

Array as Parameters of Method

By passing the array name, we are, in fact passing the reference of the array to the called function. The array in the called function now refers to the same array stored in the memory.

Program

Program Source

class Setdata{
 
    void setData(int[] v)
    {
        v[0] = 10;
        v[2] = 30;
        v[4] = 50;
    }
}

public class Javaapp {
 
    public static void main(String[] args) {
     
        int[] array_v1 = new int[5];
     
        Setdata sd = new Setdata();
        sd.setData(array_v1);
        System.out.println("array_v1 [0] = "+array_v1[0]);
        System.out.println("array_v1 [2] = "+array_v1[2]);
        System.out.println("array_v1 [4] = "+array_v1[4]);
    }
}

Leave a Comment