问题
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