Why is java.lang.String the only class for which two ways of creation exist:
1) With normal way with "new" keyword.
String s = new String("abc");
2) With String literal (which is only available with String class)
String s = "abc";
So why are there String literals and no literals for any other classes??
Short answer, because the language designers felt like it.
I guess String is such a common class that they made an exception. I'm sure there's a few other similar exceptions as well, (see Tom Hawtins comment)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With