What does “!” mean in Java?

What does “!” mean in Java?

It is an operator which is found in expressions; it operates on expressions which produce a boolean result, and negates that result. So for example in the statement

message.set Visible (! formIsComplete);

it evaluates formIsComplete as a boolean then returns the opposite, so message is only visible when the form is NOT complete.

If the part of the expression to the right of the ! doesn’t evaluate to a boolean, it’s a compiler error. So in my example above, if formIsComplete was an integer variable, that expression wouldn’t compile.

Expressions are very often found in if () statements, but I chose the example above to show that expressions can also occur in any other statement.

! in java means NOT in Logic expression.
Example: !(a==b) –>a different from b
a!=b –>a different from b
Others have been as much explicit as possible in the answer of this question.

What does "!" mean in Java
What does “!” mean in Java

This information can be found on Oracle’s Java website or even wikipedia on a page describing Java operators.

The ! operator means “not” in a boolean operation. For example, if you have a boolean variable “visible” – boolean means that it can only equal true or false by the way. Let’s say “visible”‘s value is true right now, “!visible” says not visible or the opposite of visible. So if visible is true, !visible = false.

There’s also != operator which is semantically the same as !(…==…), which you could rewrite !visible as !(visible == true) or (visible != true).

To my knowledge, there are no other ways to use ! in Java but maybe I missed one.

What does “!” mean in Java?

Negation, not.
int i = 0;
if (i != 1) –> true
bool r = true;
if (!r) –> false

In Boolean algebra, the NOT operator is a Boolean operator that returns TRUE or 1 when the operand is FALSE or 0 and returns FALSE or 0 when the operand is TRUE or 1.

Essentially, the operator reverses the logical value associated with the expression on which it operates. The NOT operator is considered one of the basic operators along with AND and OR in Boolean algebra.

Not Equal (!=)

The != operator is a comparison operator, also used in conditional expressions. It reads, “not equal”. If the compared values are not equal to each other than the expression returns true.

An example of a program that uses both the && and != operator could be a program that multiplies two numbers but only if they are both non-zero values.

int num1;
int num2;
// insert code to get input from user here.
if (num1 != 0 && num2 != 0) {
    // prints the product of num1 and num2
    System.out.println(num1 * num2); 
}

Conclusion:

One last note on the != and && operators; although it’s common to use these operators inside of conditional expressions, it’s also possible to use them when assigning to booleans.

boolean notEqual = 4 != 5;
// notEqual is true becuase 4 does not equal 5. 

boolean bothTrue = true && false; 
// bothTrue is false because a false and true evaluate to false in boolean logic.

It means Not
for example
int x=2
if(x!=3) “if x NOT equal to 3 “
printf(“True”);

What is Clickfunnels?

Jessica Morris

Hello friends, I am Jessica Morris an education blogger from New york.

You may also like...

Leave a Reply