Why in Java, (“string”).equals(var) recommended other than (var).equals(“string”)?

前端 未结 5 767
盖世英雄少女心
盖世英雄少女心 2021-01-06 04:15

I have seen most cases developers use first string and after that variable used in .equal operation. What is the reason?

5条回答
  •  执念已碎
    2021-01-06 05:06

    Because var can be null and var.equals("string") will throw NullPointerException (attempt to call of method on null). On the other hand, "string".equals(null) will just return false.

提交回复
热议问题