Difference between Initializing string with new and “ ”

前端 未结 8 1480
离开以前
离开以前 2021-01-27 08:21

Which one is correct and why:

String dynamic = new String();
dynamic = \" where id=\'\" + unitId + \"\'\";

Or

String dynamic = \" where id=\'\" + unitId + \"         


        
8条回答
  •  鱼传尺愫
    2021-01-27 09:11

    String dynamic = new String();
    dynamic = " where id='" + unitId + "'";
    

    creates a binding to an empty string, and then dumps it completely by re-initializing it.

    String dynamic = " where id='" + unitId + "'";
    

    creates the binding and leaves it.

    The first one is unnecessary. Just go with the direct string literal.

提交回复
热议问题