If String
s are immutable in Java, then how can we write as:
String s = new String();
s = s + \"abc\";
Just to clarify, when you say s = s+"abc"; That means, create a new String instance (which is composed of s and "abc") then assign that new String instance to s. So the new reference in s is different from the old.
Remember, a variable is effectively a reference to an object at some specific memory location. The object at that location stays at that location, even if you change the variable to refer to a new object at a different location.