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 :)
Who is Participating?
Jan LouwerensConnect With a Mentor Software EngineerCommented:
You have 2 conditionals happening here.

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

Open in new window

if (expression) {return 100;}

Open in new window

#1 above can be rewritten as
if (getNo() != null)
   return !compareNo(getNo(), b.getNo());
   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?
renisenbeAuthor Commented:
Thanks, very clear.
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.