what is the advantage of string object as compared to string literal

后端 未结 6 2004
你的背包
你的背包 2021-01-02 16:59

i want to know where to use string object(in which scenario in my java code). ok i understood the diff btwn string literal and string object, but i want to know that since

6条回答
  •  不知归路
    2021-01-02 17:39

    Don't use a new String object if you know what the string is. For example:

    String str = new String("foo"); // don't do this
    

    You are thus creating an unnecessary object - once you have a String object created from the literal, and then you create another one, taking the first one as constructor argument.

提交回复
热议问题