Checking for string contents? string Length Vs Empty String

前端 未结 13 947
野趣味
野趣味 2020-12-17 10:14

Which is more efficient for the compiler and the best practice for checking whether a string is blank?

  1. Checking whether the length of the string == 0
13条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 10:46

    In this case, directly checking the length is faster, because it avoids the overhead of constructing the new empty string.

    @DerekPark: That's not always true. "" is a string literal so, in Java, it will almost certainly already be interned.

提交回复
热议问题