Java String
s are immutable.
In your first example, you are changing the reference to the String
, thus assigning it the value of two other Strings
combined: str + " Morning"
.
On the contrary, a StringBuilder
or StringBuffer
can be modified through its methods.