Java if-else shorthand

if (getNo() != null ? !compareNo(getNo(), b.getNo()) : getNo() != null) {
  return 100;
}

Assuming getNo() is not null, does this mean that "return 100" runs if !compareNo(getNo(), b.getNo()) evaluates to true? What happens to the last expression (after the :)
renisenbeAsked:
Who is Participating?
 
Jan LouwerensConnect With a Mentor Software EngineerCommented:
You have 2 conditionals happening here.

1.
getNo() != null ? !compareNo(getNo(), b.getNo()) : getNo() != null

Open in new window

2.
if (expression) {return 100;}

Open in new window


#1 above can be rewritten as
if (getNo() != null)
{
   return !compareNo(getNo(), b.getNo());
}
else
{
   return getNo() != null;  // This will actually always return true, because this is the same expression in the main "if" clause
}

Open in new window


Does seeing it this way make it more clear what's going on?
0
 
renisenbeAuthor Commented:
Thanks, very clear.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.