Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of ! in Java syntax

Tags:

java

syntax

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

return(!weekday || vacation);
like image 876
Will Avatar asked Jul 28 '11 15:07

Will


People also ask

What is mean by :: in Java?

The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions.

What is the basic syntax of Java?

Java Identifiers All Java components require names. Names used for classes, variables, and methods are called identifiers. All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). After the first character, identifiers can have any combination of characters.

What does :: new mean in Java?

The Java new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.


4 Answers

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 trueand vacation is false.

like image 29
T.J. Crowder Avatar answered Oct 19 '22 05:10

T.J. Crowder


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.

like image 145
Thomas Owens Avatar answered Oct 19 '22 06:10

Thomas Owens


! 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.

like image 5
cwallenpoole Avatar answered Oct 19 '22 05:10

cwallenpoole


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

like image 1
CoolBeans Avatar answered Oct 19 '22 07:10

CoolBeans