Java 布尔值前的感叹号是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18940910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what does exclamation mark before the boolean?
提问by komal gupta
if(!checkSecondNum())
Where checkSecondNum is a method of boolean return type and why we used exclamatory sign in if statement please describe me in brief.
checkSecondNum 是一种布尔返回类型的方法,为什么我们在 if 语句中使用感叹号,请简要描述我。
回答by Suresh Atta
!
called as boolean negation or Logical complimentand makes just inverts the result.
!
称为布尔否定或逻辑恭维,并且只反转结果。
If your method checkSecondNum()
returns false
then
如果您的方法checkSecondNum()
返回,false
则
if(!checkSecondNum())
Becomes true
.
成为true
。
!false
equals to true
!false
等于 true
回答by Aurand
You should take a look at the summary of Java operators:
你应该看看Java操作符的总结:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
In this specific case, !
is a logical NOT
.
在这种特定情况下,!
是一个合乎逻辑的NOT
.
回答by Vallabh Patade
!
is logical not operator.
That means when checkSecondNum()
returns false
execution will enter the if block.
!
是逻辑非运算符。这意味着当checkSecondNum()
返回false
执行将进入 if 块。
!false = true
and
!true = false
!false = true
和
!true = false
回答by Cobain
I think your progarm just wants to do the thing that when checkSecondNum()
is not true, then executes the code in if{}
. So your should use a !
.
我认为你的程序只是想做这样的事情,whencheckSecondNum()
不是真的,然后在if{}
. 所以你应该使用一个!
.
回答by kammy
- ! is Logical NOT operator.
- It will work reverse with your condition.
See simple example Given Below.
example-
n1=!true // it returns false
n2=!false // it returns true
n3=!"Cat" // it returns false - Hope you got meaning of NOT operator.Same in your case.
- !是逻辑非运算符。
- 它会与您的情况相反。请参阅下面给出的简单示例。例子-
n1=!true // it returns false
n2=!false // it returns true
n3=!"Cat" // it returns false - 希望您理解 NOT 运算符的含义。在您的情况下也是如此。