Syntax question on listing 33.4


Please bear with me, I originally learned Java about 14 years ago (actually certified on version 2), but have used it seldom until just recently.

What exactly does this do?

int flags = shouldCreate ? 0 : PendingIntent.FLAG_NO_CREATE;

Unfortunately, Google doesn’t help for this type of question, and I haven’t seen that sort of syntax in any other programming language I’m familiar with.


Nevermind (unless I’m wrong), from a similar construct in listing 33.5 I’ve decided that it’s a really fancy “if” statement.


Google “ternary operator” because that is what is is
It is also a fancy type of if statement

How it works

is equivalent to

{flags =0;}
{flags = PendingIntent.FLAG_NO_CREATE;}

I assume that passing a flag with a value of 0 is standard and good practice and
will result in the default working PendingIntent being created

and I also assume that someone passes an actual named constant integer such as PendingIntent.FLAG_NO_CREATE only to obtain
different functionality or to stop the PendingIntent

and my 3rd assumption is that none of the named constants in PendingIntent have a value of 0 because using an actual Integer
instead of a named constant would be BAD practice of the type that none of the authors of this book would EVER be guilty of

I think that sometimes when learning this sort of stuff sometimes you just need to read between the lines if you can’t find the answer elsewhere