How many String object..?

前端 未结 6 1336
礼貌的吻别
礼貌的吻别 2021-01-18 13:36

Me and my friend was discussing on Strings and we stuck on this:

String str = \"ObjectOne\"+\"ObjectTwo\";

He says total three Object will

6条回答
  •  广开言路
    2021-01-18 14:39

    String str1 = "ObjectOne";
    String str2 = "ObjectTwo";
    str1 = str1+str2;
    

    In above case three objects will be created.

    But when you define like

    String str = "ObjectOne"+"ObjectTwo";
    

    then only one object will be created. Compiler optimizes it.

提交回复
热议问题