In this program why can arrays values stored in index be changed in both main me
ID: 666247 • Letter: I
Question
In this program why can arrays values stored in index be changed in both main method and mystery method, when the array is declared in main as a local variable? When the array is printed in the main method, the chnage took in the mystery method is present, why is this? is it a property of arrays?
Public class ReferenceMystery {
public static void main(String[] args) {
int x = 0;
int[] a = new int[4];
x = x + 1;
mystery(x, a);
System.out.println(x + “ “ + Arrays.toString(a));
x = x + 1;
mystery(x, a);
System.out.println(x + “ “ + Arrays.toString(a));
}
public static void mystery(int x, int[] a) {
x = x + 1;
a[x] = a[x] + 1;
System.out.println(x + “ “ + Arrays.toString(a));
}
}
Explanation / Answer
I explained each statement in your program. So you can understand what those statments
meant, when they will be executed and how the values are changed.
Program explanation:
step 1:int x=0;
Here x value is set to zero
step 2:int[] a = new int[4];
Here the array named as a is declared. It can store 4 integer values.
step 3: x = x + 1;
Here the value of x is incremented by 1. x= 0 + 1
Now x=1
step 4: mystery(x, a);
Here the function Mystery is called. The value of x and array a is passed as
arguments. x=1 and a is [0,0,0,0].
The control transfered to mystery function.
Step 5: mystery(int x, int[] a)
The variables x and a[] here are local to mystery function only. From the function call
values assigned to these varibles.
Here x=1 and a is [0,0,0,0]
Step 5a: x = x + 1;
The value of x is incremented by one. x= 1 + 1.
Now x=2. This is inside mystery function only. it will not affect the value of x
in main function.
Step 5b: a[x] = a[x] + 1;
It means a[2]=a[2]+1 = 0 + 1
Now a[2] =1 and
array a becomes [0,0,1,0]
Step 5c: System.out.println(x + " " + Arrays.toString(a));
This statement prints the value of x as 2.
It also converts the value os array as string to display.
a[0,0,1,0] is printed.
Now mystery function execution finished and control will send back to mystery function.
step 6: System.out.println(x + " " + Arrays.toString(a));
this is main function statement. In the main function x value is still 1 only. not become 2.
Because x is passed by value.
but the array a value is updated. When the array is passed as argument, its address is sent to
the calling function and not just values.
Mystery function places the value of its local variable a in the same address.
step 7: x = x + 1;
x value becomes 2 in main
step 8: mystery(x, a);
again mystery is called with x value is 2, a is [0,0,1,0]
step 9: Inside mystery, the arguments received as x=2, and a[0,0,1,0]
Then x = x+1 and x becomes 3
a[3]= a[3] +1 and a[3] becomes 1
So a is [0,0,1,1] and x and a are printed. Control returned to main.
step 10: System.out.println(x + " " + Arrays.toString(a));
This statement is inside main. x value is 2 as we know. and a is [0,0,1,1] are printed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.