In this article, we will learn how do you write the not equals sign on Java.
Understanding != in Java
Now since we aim at understanding inequality, we need to first understand what equality is.
public static void main(String[] args) { | |
int a = 5; | |
int b = 5; | |
System.out.println(a==b); | |
} |
true
Very easy, right,
public static void main(String[] args) { | |
int a = 5; | |
int b = 5; | |
System.out.println(a!=b); | |
} |
false
Understanding !.equals() in Java
Again we’ll first understand .equals() to understand !.equals().
We know Strings are objects, so we can use .equals() with Strings as given below.
public static void main(String[] args) { | |
String a= "Java"; | |
String b= "Python"; | |
System.out.println(a.equals(b)); | |
} |
false
Here, Java is not equal to Python, so a.equals(b) will be false.
Let’s try !a.equals(b) now.
public static void main(String[] args) { | |
String a= "Java"; | |
String b= "Python"; | |
System.out.println(!a.equals(b)); | |
} |
true
Here, Java is not equal to Python, so !a.equals(b) or simply put checking if a is not equal to b will be true.
In this java tutorial, we learned how do you write the not equals sign on Java.
I hope this article helped you.
In case you have doubts, I suggest you take a live 1:1 lesson on this subject from me. I am an Oracle-certified tutor who would love to teach this stuff to you directly.
Aniket Malik