Picture of the Action

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes.

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, not a copy of the object.

In the example, the zeroElt() method gets a reference to the array object. Since the parameter x refers to the array, the method may change the array by using x. The picture shows this.

This is call by value, because when the method call cng.zeroElt(value, 0) is executed, the parameter x of the method gets the value held in the variable value, which is a reference to the array.

QUESTION 7:

Since the method zeroElt() has a reference to the array, can it change individual array elements?