. Advertisement .
..3..
. Advertisement .
..4..
The “!=” operator is something that all of us make contact with the moment we start learning to program. However, there is another operator, the “=!” in Java, that makes many people confused due to how similar it is to “!=”.
To make it easier for you, we prepared this article to showcase the difference between the != and =! operator in Java.
The Difference Between The != And =! Operator In Java
The != Operator In Java
What we call the != operator has the meaning of “not equal to”. As you can guess, its main job is to function as an equality operator, checking whether or not two operands are equal. It is a Boolean-type operator.
In other words, it only has two return values, True and False. You do need to keep in mind that it only returns true if the two operands are not equal. Otherwise, it will return false.
public class Test{
public static void main(String[] args){
int x = 5,y = 5;
System.out.println("x = "+x);
System.out.println("y = "+y);
boolean result = x!=y;
System.out.println(result);
x = 10;
System.out.println("x = "+x);
result = x!=y;
System.out.println(result);
}
}
With this code, you will get two outputs for the != operator, both true and false. The first return value is false, as both x and y are assigned the same value, then added in another value of themselves.
However, once we change the value of x, the two operands are no longer equal, so the result printed out will not be false but true.
Output:
a = 5
b = 5
false
a = 10
true
There is something interesting that you should keep in mind: the “x != y” is actually !(x == y). In other words, it is the reverse of the equal operator “x == y” that we have been using since we first learn about programming.
So, the example above can be changed into the following:
public class Test{
public static void main(String[] args){
int x = 5,y = 5;
System.out.println("x = "+x);
System.out.println("y = "+y);
boolean result = !(x == y);
System.out.println(result);
x = 10;
System.out.println("x = "+x);
result = !(x == y);
System.out.println(result);
}
}
The output will stay the same.
The =! Operator In Java
Despite only changing the position of “!”, the whole operator is turned into something completely different. This “=!” operator is responsible for assigning to a variable its reverse Boolean value.
In other words, the first thing “x =! y” will do is invert the value of y, then assigns this new value to a. That is why you can write this code in this way: x = !b to remind yourself of the way it operates. Let’s see how it works with our example:
public class Test{
public static void main(String[] args){
boolean x = false, y = false;
System.out.println("x = "+x);
System.out.println("y = "+y);
x = !y;
System.out.println(x);
y = true;
System.out.println("y = "+y);
x = !y;
System.out.println(x);
}
}
Output:
x = false
y = false
true
y = true
false
You can make use of this operator’s nature to manipulate conditional code by adding conditional statements. This boolean negation process is one of the most used in any programming language.
Conclusion
After reading through this, we hope that you can now see exactly the difference between the != and =! operator in Java. Remember that you should never mix them up, as they are built specifically for different purposes.
Even when they use the same components, how these components are aligned makes them completely different.
Leave a comment