简体   繁体   English

的意思 !在Java语法中

[英]Meaning of ! in Java syntax

In the line below where it shows return(!variable); 在下面的行中显示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 . 逻辑上, !true == false!false == true

Like Platinum Azure said in the comments, this operator can only be applied to boolean types. 就像Platinum Azure在评论中所说,这个运算符只能应用于boolean类型。

The ! ! is a boolean NOT operator, defined in Section 15.15.6 of the Java Language Specification. 是一个布尔NOT运算符,在Java语言规范的第15.15.6节中定义。 It makes true false and false true . 它使true falsefalse 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 . 那么是什么return语句做的是返回,这将是一个布尔true如果任一 weekdayfalse (“不是工作日”)或( ||vacationtrue It will be false if weekday is true and vacation is false . 这将是false ,如果weekdaytruevacationfalse

! means negation. 意味着否定。 Basically, "Ok, so whatever follows, if it is true, return false, if false return true." 基本上,“好吧,所以无论如何,如果是真的,如果错误返回真实,则返回false。” ( ! will only work on booleans in Java) In this case, your return becomes: !仅适用于Java中的布尔值)在这种情况下,您的返回将变为:

return that it is not a weekday or that it is vacation. 返回它不是工作日或是假期。

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

It means when NOT weekday (boolean false). 这意味着NOT weekday (布尔值假)。 ! stands for negation. 代表否定。

It's a negation. 这是一个否定。 ! means not . 意思not

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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