Java Strings: “String s = new String(”silly“);”

前端 未结 23 2815

I\'m a C++ guy learning Java. I\'m reading Effective Java and something confused me. It says never to write code like this:

String s = new String(\"silly\");         


        
23条回答
  •  误落风尘
    2020-11-22 14:46

    Strings are special in Java - they're immutable, and string constants are automatically turned into String objects.

    There's no way for your SomeStringClass cis = "value" example to apply to any other class.

    Nor can you extend String, because it's declared as final, meaning no sub-classing is allowed.

提交回复
热议问题