TheBlueCat TheBlueCat - 8 months ago 24
Java Question

Java basics exclamation mark

I'd like to confirm the meaning of

before a boolean expression in a control statement means the opposite:

For example:

if (!NetworkConnected())

Does that mean "if the network is not connected"?


Yes it does mean the logical opposite. It works even with equals operator.

Assuming your method return a basic bool type

// means the Network is NOT connected
if (!NetworkConnected()) 

This is equivalent to

if (NetworkConnected() != true) 

So logically means

if (NetworkConnected() == false) 

Now assuming you method return a Boolean (indeed a real object), this means

// means the Network is NOT connected
if (! Boolean.TRUE.equals(NetworkConnected());


if (Boolean.FALSE.equals(NetworkConnected());