Why is myString.equals(“aString”); different from “aString”.equals(myString);?

前端 未结 3 1518
我在风中等你
我在风中等你 2020-12-19 05:39

I heard several times that in using boolean equals(Object o) to compare Strings, it\'s better to put the constant on the left side of the function

3条回答
  •  情深已故
    2020-12-19 06:24

    This is a defensive technique to protect against NullPointerExceptions. If your constant is always on the left, no chance you will get a NPE on that equals call.

提交回复
热议问题