-1

What do the curly braces with the number 42 do to the array in this scenario?

Class Example {
    static void g(int[] array) {
        array[1] = 19;
        array = new int[] {42};
    }

    static void f() {
        int [] array = new int[1];
        g(array);
    }
}

2 Answers2

0
array = new int[] {42};

This line creates a new integer array with size 1, whose only element is 42, and assigns it to array. It is equivalent to

array = new int[1];
array[0] = 42;
Louis Wasserman
  • 182,351
  • 25
  • 326
  • 397
0

this is actually creating array with size of 1 and only element that is array[0]=42

shouzeb hasan
  • 21
  • 1
  • 2