Difference between Initializing string with new and “ ”

前端 未结 8 1485
离开以前
离开以前 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:02

    I guess the below will be good

    String str; // Just declares the variable and the default will be null. This can be done in global scope or outer scope
    
    str = "where id" + unitID + "'"; // Initializing string with value when needed. This will be done in the inner scope.
    

    If declaration and initialization done in a line where the initialization contains dynamic text (unitID in your case) you can't do it globally. If Scope of the variable is not an issue then u may go ahead. Cheers!!

提交回复
热议问题