How String instance methods such as isEmpty and toUpperCase being called without creation of String Object.
"myString".toUpperCase();
since toUpperCase is not static method, how its being called without creation of new String()?
How String instance methods such as isEmpty and toUpperCase being called without creation of String Object.
"myString".toUpperCase();
since toUpperCase is not static method, how its being called without creation of new String()?
A string literal like "myString"
does create a String
object¹, even if you don't explicitly see a new String()
call in the code. Consider String s = "myString";
which couldn't work otherwise! So you can call any method on it as usual.
¹ Or reuses an existing one, see string pooling.