Eng.Fouad Eng.Fouad - 4 months ago 7
Java Question

!flag has two meanings in java?

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

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