简体   繁体   中英

Meaning of ! in Java syntax

In the line below where it shows return(!variable); what does the exclamation mark do to the variable?

return(!weekday || vacation);

The ! character is logical negation. It's formal name is, I believe, "logical not". Logically, !true == false and !false == true .

Like Platinum Azure said in the comments, this operator can only be applied to boolean types.

The ! is a boolean NOT operator, defined in Section 15.15.6 of the Java Language Specification. It makes true false and false true . So what that return statement is doing is returning a boolean which will be true if either weekday is false ("not weekday") or ( || ) vacation is true . It will be false if weekday is true and vacation is false .

! means negation. Basically, "Ok, so whatever follows, if it is true, return false, if false return true." ( ! will only work on booleans in Java) In this case, your return becomes:

return that it is not a weekday or that it is vacation.

您可以在Java教程中回答所有操作员问题。

It means when NOT weekday (boolean false). ! stands for negation.

It's a negation. ! means not .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM