What Does an Exclamation Mark Mean in Java

What does an exclamation mark mean in Java?

Yes it does mean the logical opposite. It works even with equals operator.

Assuming your method return a basic bool type

// means the Network is NOT connected
if (!NetworkConnected())

This is equivalent to

if (NetworkConnected() != true) 

So logically means

if (NetworkConnected() == false) 

Now assuming you method return a Boolean (indeed a real object), this means

// means the Network is NOT connected
if (! Boolean.TRUE.equals(NetworkConnected());

or

if (Boolean.FALSE.equals(NetworkConnected());

Meaning of ! in Java syntax

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.

What does an exclamation mark before a variable mean in JavaScript

! is the logical not operator in JavaScript.

Formally

!expression is read as:

  • Take expression and evaluate it. In your case that's variable.onsubmit
  • Case the result of that evaluation and convert it to a boolean. In your case since onsubmit is likely a function, it means - if the function is null or undefined - return false, otherwise return true.
  • If that evaluation is true, return false. Otherwise return true.

In your case

In your case !variable.onsubmit means return true if there isn't a function defined (and thus is falsy), otherwise return false (since there is a function defined).

Simply put - !variable means take the truth value of variable and negate it.

Thus, if (!variable) { will enter the if clause if variable is false (or coerces to false)

In total

if (!variable.onsubmit || (variable.onsubmit() != false)) {

Means - check if variable.onsubmit is defined and truthy (thus true), then it checks if calling onsubmit returns a result that coerces to true. In a short line it checks if there is no onsubmit or it returns true.

Next time, how do I find this myself?

  • MDN has a list of operators here.
  • The language specification specifies such operators, though being the official specification it does contain some jargon which might be hard to understand.

What does the exclamation mark before class path do in ProGuard?

It means the same thing that ! means in many other programming languages. It negates the selection that follows. So in your example, the keep directive will apply to everything that is not in the following package/class selection.



Related Topics



Leave a reply



Submit