Best way to check for null values in Java?

后端 未结 16 1203
傲寒
傲寒 2020-12-04 16:30

Before calling a function of an object, I need to check if the object is null, to avoid throwing a NullPointerException.

What is the best way to go abou

16条回答
  •  渐次进展
    2020-12-04 17:14

    If at all you going to check with double equal "==" then check null with object ref like

    if(null == obj) 
    

    instead of

    if(obj == null)
    

    because if you mistype single equal if(obj = null) it will return true (assigning object returns success (which is 'true' in value).

提交回复
热议问题