Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an Object is null at the same time as a value of one of it's fields

Tags:

java

Which of these would be correct?

if(dialog != null && dialog.isShowing){}

if(dialog.isShowing && dialog != null){}

if(dialog != null){
   if(dialog.isShowing){}
}
like image 527
Maximus Avatar asked Apr 27 '11 14:04

Maximus


1 Answers

The && operator is called a short circuit operator. This means once the result is known. i.e., false && x is always false it doesn't evaluate the remaining expressions.

A variation on this is using the || short circuit, OR operation, like:

if(text == null || text.isEmpty())
like image 110
Peter Lawrey Avatar answered Oct 09 '22 05:10

Peter Lawrey