Justify the reason that Java Strings are immutable by showing how Strings might be packed together in the heap to help students resolve incomplete understandings in their mental model.

  • Extra reinforcement of this concept is helpful as students struggle a lot with the immutability of Java Strings.
  • Activity:
    • Display the following code public static void main(String[] arg){ String firstString = new String("abc"); String secondString = new String("xyz"); String thirdString = firstString.concat("efg"); }
    • Draw a picture of what this might look like in memory.
      • An illustration of the relationship between two strings in the main method and The Heap.
    • Explain that when we want to add 123 to the end of abc, we don’t have room after it in the heap. So we need to create an entirely new String.
      • An illustration of the relationship between three strings in the main method and The Heap.

More about this tip

External Source

Interview with Suzy Crowe

Other Tips By