Back to Java Questions Index

Cosmic Q&A

What does it mean when we say String is immutable in Java?

Answer

When we say String is immutable, it means that any operations we perform on a String, JVM will create a new String in memory. Compare this with an ArrayList for example. When we add a new data element into an ArrayList, JVM just updates the data in the existing array list. This is known as Mutability. When we perform operations on string like converting to upper case etc, JVM will create a new String and old string will become eligible for Garbage collection if there are no references pointing to it.


Share this page:

Report Error in Content