Java equals() ordering

后端 未结 6 947
执念已碎
执念已碎 2021-01-12 02:55

If I try to do a .equals() on a null string in java, a null pointer exception will be thrown. I am wondering, if I am trying to compare if a string is equal to

6条回答
  •  萌比男神i
    2021-01-12 03:22

    This is a standard Java idiom jokingly called a Yoda condition.

    Personally I prefer to handle the null case explicitly, but the Yoda way is used a lot and any experienced Java programmer should be able to understand what is going on immediately. It's fine to use.

提交回复
热议问题