Show students explicitly how objects and variables might have other names (i.e., aliasing) to help students understand that variable names don’t have to match parameter names because students often find this confusing.

  • 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)); }

      Example diagram of more complex variable aliasing
  • 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]); }

    Example diagram of variable aliasing for above code

More about this tip

Tags

External Source

Interview with Serita Nelesen.