Picture of the Action
It looks as if there is a contradiction here:
- In the previous example,
the method COULD NOT change the int variable in main().
- In this example,
the method CAN change the int array in main().
There is actually no contradiction here.
Remember that an array is an object.
When an object is a parameter, the called method gets a reference to the object.
That is, it gets a way to find the object in memory, but
no new object is created.
In the example, the called method gets a reference to the array object.
There is only one array, so any changes will affect
that array.
The picture that shows this.
When the method call cng.zeroElt( value, 0 )
is executed,
the parameter x of the method
gets the value of the of the variable,
which is a reference to the array.
|