Java

What is the Difference Between the != and =! Operators in Java

The symbol used to perform operations is called an operator. In Java, the “!=” is a Comparison Operator utilized to determine whether two operands are equal or not by comparing them, while the “=!” operator combines two operators; the first is an Assignment operator “=”, and the second is a logical Not operator “!” that operates on boolean values. It is also utilized in the inversion of boolean values.

This post will explain the difference between != and =! Operators in Java.

What is the Difference Between the != and =! Operators in Java?

!=” and “=!” are two separate Java operators. These two operators cannot be compared because they serve different functionalities. The “!=” operator compares two operands, whereas the “=!” operator inverses the result of boolean values. Its representation only gives a look-alike feel.

Now, we will discuss both methods and their usage one by one!

What is “!=” Operator in Java?

The “!=” operator is also called the “not equal to” Java operator. It is used to evaluate whether or not two operands are equal while using conditional statements. This operator returns boolean values, where “true” indicates that the values are compared and are not equal, and “false” refers to its opposite case when both values are equal.

How to Use “!=” Operator in Java?

Follow the below-given syntax for comparing the variable value “a” with “b” using the “!=” (not equal) operator:

a!=b

 
Example 1

In this example, we will create two integer type variables, “a” and “b” and initialize them:

int a = 20;
int b = 23;

 
Print the original values of variables “a” and “b” on the console with the help of the “System.out.println()” method:

System.out.println("The value of a = "+a);
System.out.println("The value of b = "+b);

 
Then, we will check whether the value of “a” is equal to the value of “b” using the “!=” operator. It will return a boolean value “false” if both values are equal:

boolean output = a!=b;

 
Lastly, print out the resultant value on the console:

System.out.println(output);

 

The output shows the boolean value “true”, which indicates that the values of “a” and “b” are not equal:


Example 2

Now, we will perform the same operation using the following syntax:

boolean output =!(a==b);

 
It first checks the equality of “a” and “b” variable values using the comparison operator “==” and then negates the result.

Finally, print the output on the console:

System.out.println(output);

 

Output


Now, let’s move to another example where we will see how the mentioned operator helps to find equality under the specified conditions.

Example 3

Here, we will consider the same variables “a” and “b” with their values as in the previous examples. We will use the conditional “if” statement to check whether the values of both variables are equal or not and print out the added statements:

if(a!=b) {
    System.out.println("The value of ‘a’ is not equals to the value of ‘b’");
}else{
    System.out.println("The value of ‘a’ is equals to the value of ‘b’");
}

 

The output shows that the values of “a” and “b” are not equal:


Now, head toward the working of the “=!” operator in Java.

What is “=!” Operator in Java?

The “=!” operator is the combination of the two operators, the Assignment operator “=” and the logical not “!” operator. The logical not operator is used to convert the original value, while the assignment operator is used for the assignment of the value.

In order to convert the output of boolean values, you can use the “=!” operator. It converts the boolean value and then assigns it to the second operand.

How to Use “=!” Operator in Java?

The syntax for using the “=!” operator is given as follows:

x= !y

 
Note: The correct expression is “x= !y” not “x=!y”; it is a bit confusing. The expression “x= !y” shows that the boolean value of “y” is first inverted, and then it is assigned to “x”.

Example

In this example, we have two boolean type variables, “x” and “y”, with the following values:

boolean x = true;
boolean y = true;

 
Print the values of the created variables on the console using the “System.out.println()” method:

System.out.println("The boolean value of x = "+x);
System.out.println("The boolean value of y = "+y);

 
Now, we will convert the boolean value of “x” with the help of the “=!” operator:

x= !y;

 
The “y” will first invert its value from “true” to “false”, and then it is assigned to “x”. Print the updated value of “x” on the console:

System.out.println("Now the value of x = " +x);

 

Output


We have provided all the basic information about the “!=” and “=!” operators in Java.

Conclusion

Both “!=” and “=!” are Java operators used to perform different functionalities. The “!=” operator is a comparison operator that compares the equality of two operands to determine if they are equal or not, whereas the “=!” is the combination of two operators, an Assignment operator “=”, and the logical not operator “!” that operates on boolean values. In this post, we discussed the difference between != and =! operators in Java and their usage.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.