It’s incredibly important to go over this information with student because it’s a very tough concept for them to wrap their mind around.
You can use the following examples in Java to help students with this concept.
This first example uses a primitive data type so that it’s clear that a copy is made.
public void squareArea (int length){ return length * length; } public static void main(String[] args){ int squareLength = 4; System.out.println(squareArea(squareLength)); }
Now, compare and contrast the first example to a more complex reference parameter like the dwarfs example below:
public void changeArray (String[] inputs){ inputs[0] = "Dopey"; inputs[1] = "Sleepy"; System.out.println(inputs[0] + " " + inputs[1]); } public static void main(String[] args){ String[] names = ["Grumpy", "Happy"]; changeArray(names); System.out.println(names[0] + " " + names[1]); }