Check if variable null before assign to null?

后端 未结 8 1179
清歌不尽
清歌不尽 2021-01-05 16:55

Is variable assignment expensive compared to a null check? For example, is it worth checking that foo is not null before assigning it null?

if (foo != null)          


        
8条回答
  •  天命终不由人
    2021-01-05 17:22

    If you have a decent compiler they will generate identical code. If you have a crappy compiler the one with the if will be worse. On 2009 hardware assignments to variables are very cheap, and conditional branches can sometimes be expensive.

提交回复
热议问题