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

前端 未结 3 1516
我在风中等你
我在风中等你 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:23

    This is poor design, because you are hiding NullPointerExceptions. Instead of being alerted that string is null, you will instead get some weird program behaviour and an exception being thrown somewhere else.

    But that all depends if 'null' is a valid state for your string. In general 'null's should never be considered a reasonable object state for passing around.

提交回复
热议问题