Calling equals on string literal

后端 未结 8 1275
再見小時候
再見小時候 2020-12-29 04:32

I just was tidying my code a bit and there was this piece:

String saving = getValue();
if(saving != null && saving.equals(\"true\")){
   // do someth         


        
8条回答
  •  借酒劲吻你
    2020-12-29 05:08

    if("true".equals(saving)){
       // do something
    }
    

    This is very safe and good practice. String "true" will never be null. So you will never compare your String to null. This piece of code is perfectly fine

提交回复
热议问题