jeudi 30 janvier 2020

Way to check if a thread is null or not (in Java), does operand position makes difference [duplicate]

NOTE: I went through these but didn't get my answer Best way to check for null values in Java? and (obj == null) vs (null == obj)?

I was studying this Android [Java] official documentation on Background Tasks -> Run code on a thread pool thread -> Interrupt running code, and the null check code in the sample is:

if (null != thread) {
    thread.interrupt();
}

which is different from what we usually see/use:

if (object != null) {
    //do something;
}

So, my question is that:

Does it make any difference (like helping avoid null pointer or something) if we write "null != thread" instead of "thread != null" or Google Official documentation is just randomly swapping the operands without any benefit?

EDIT:

  1. I am asking about != and not ==. In case of ==, the programmer may do assignment(=) instead of comparison(==). But that is not the case in !=.
  2. I am talking about Java and not C language. So, that assignment and comparison confusion doesn't apply here.

Aucun commentaire:

Enregistrer un commentaire