TheBlueCat TheBlueCat - 1 year ago 103
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"?

Answer Source

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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download