Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is String the only class for which literals exist in Java?

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??

like image 775
Silent Warrior Avatar asked Jul 05 '26 11:07

Silent Warrior


1 Answers

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)

like image 91
Glen Avatar answered Jul 06 '26 23:07

Glen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!