What does ! mean in Java?

The ! operator is also known as negation or NOT operator and it belongs to the category of Java unary operators. It is used to invert the consequences of boolean values i.e. it over turns the true values to false and false values to true or more concisely we can say that it is used to reverse the logical state of an operand. In java, the ! operator has multiple use cases the most common among them are while loop and if-else statements.

This write-up provides a detailed understanding of the following aspects regarding negation operator:

So let’s begin!

What is Unary operator in Java

The operator that requires only one operand to perform some specific operations is known as the unary operator in java. Most frequently used unary operators are: increment operator (++), logical not operator (!), decrement operator (–), and so on.

What does “!” means in Java

In programmatically terms the “!” operator is referred to as a “logical not” and it is used to invert a boolean value. For example: if the value is true then it will turn the value to false and vice versa.

Syntax of “!” Operator in Java

The basic syntax of the logical not or negation operator is shown in the below-given code snippet:


How to Use “!” Operator in Java

Let’s consider a very simple example for a profound understanding of how to use ! operator in java.


In this example we initialize a variable flag with true. Firstly, we print the actual value of flag and then we print the flag’s value using logical not ! operator:

boolean flag = true;
System.out.println("Actual value: " + flag);
System.out.println("Value using logical not operator: " + !flag);

The complete code snippet and its output are shown in the following snippet:

The output shows the use of logical not operator ! provides a converse result.

How to Use “!” Operator in Conditional Statements

In java, normally the if block executes if the specified condition is true and the else block executes if the specified condition is false. However, by using the ! operator we can reverse the logic.


The below-given code snippet provides an in-depth view of how to use ! operator in if-else statements:

int number1 = 50;
int number2 = 100;
if( !(number1 < number2))
System.out.println("Number2 is Greater than Number1");
System.out.println("Number2 is less than Number1");

In this example we have two values 50 and 100, logically the if block should execute if the condition is true i.e. if (50 < 100). However, we specify the ! operator in the if statement so, the if block will execute when the condition is false i.e. if (50 > 100):

The output verifies that the else block gets executed which shows the appropriateness of the ! operator.

How to Use “!” Operator in While Loop

Another frequently used use-case of ! operator is while loop, where the logical Not operator specifies that the loop must execute until the specified condition stays false:


Let’s consider the below piece of code for a profound understanding of how to use ! operator in the while loop:


int number1 = 0;
while(number1 !=10)
System.out.println("Number: " + number1);

The above-given piece of code provides the following output:

The output authenticates that the loop executes until the condition is false and it stops its working when the condition becomes true (i.e. 10=10).


The “!” operator is a unary operator known as the “logical not” operator and it is used to inverse a boolean value. Using ! operator the logic of if-else statements can be reversed i.e. the if block executes when the specified condition is false and else block executes when the specified condition is true. The ! operator can be used in the while loop to iterate a loop if the condition is false. This write-up provides a detailed overview of what does ! means, and what are the use-cases of the ! operator in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.