java logical not operator [duplicate]

a 夏天 提交于 2019-12-12 06:07:14

问题


This is a silly question but I just want to understand it clearly before I start using it.

if (!ServiceHelpers.DISCOVER) {
  ServiceHelpers.discover(MainActivity.this, peerList);
}

I would like to know what !ServiceHelpers.DISCOVER mean?


回答1:


! operator inverts the value of a boolean.

In this case the boolean is ServiceHelpers.DISCOVER.

If it's value is true the ! operator will make it false or vice-versa.




回答2:


! (logical not)

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

From Javadocs,

!Logical complement operator;

inverts the value of a boolean

if (!ServiceHelpers.DISCOVER) 

is same as

if( Not True)

meaning, ServiceHelpers.DISCOVER is not True the enter inside If




回答3:


! is a logical not operator in java.

Code:

 boolean a = true; // --> variable 
   if(! a)
   {
      System.out.println("Not A i.e. A if false");
   }

if a = true, then (!a) evaluates to (not a) i.e (not true) i.e (false) and if condition is false, then it doesn't evalutes the if condition.



来源:https://stackoverflow.com/questions/33932074/java-logical-not-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!