- 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.
- 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.