!flag has two meanings in java?

Eng.Fouad picture Eng.Fouad · Jul 12, 2011 · Viewed 18.8k times · Source
boolean flag = false;
if(!flag) System.out.println(!flag); // prints true

I wonder why !flag being considered as false when it's a conditional parameter passed to if statement and as true elsewhere?

Answer

dlev picture dlev · Jul 12, 2011

It's not. if (boolean expression) { statement } means "execute the statement if boolean expression is true." Since flag = false, !flag == true. Always.