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

前端 未结 23 2392

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:24

    First, you can't make a class that extends from String, because String is a final class. And java manage Strings differently from other classes so only with String you can do

    String s = "Polish";
    

    But whit your class you have to invoke the constructor. So, that code is fine.

提交回复
热议问题