Not equal
to (!=) is a conditional or relational operator, used to check if one operand
is not equal to other operand or not.
Syntax
a != b
Above
snippet return true, if a is not equal to b, else false.
NotEqualToOperatorDemo.java
package com.sample.app;
public class NotEqualToOperatorDemo {
public static void main(String[] args) {
boolean b1 = (10 != 10);
boolean b2 = (10 != 11);
System.out.println("(10 != 10) is evaluated to : " + b1);
System.out.println("(10 != 11) is evaluated to : " + b2);
}
}
Output
(10 != 10) is evaluated to : false
(10 != 11) is evaluated to : true
Since !=
operator return a Boolean value (true or false), you can use this in decision
making statements and in loops.
!=
operator in decision making statements
if (a != 10) {
System.out.println("a is not equal to 10");
} else {
System.out.println("a is equal to 10");
}
Find the
below working application.
NotEqualToOperatorInConditionalStatements.java
package com.sample.app;
public class NotEqualToOperatorInConditionalStatements {
public static void main(String[] args) {
int a = 10;
if (a != 10) {
System.out.println("a is not equal to 10");
} else {
System.out.println("a is equal to 10");
}
a = 11;
if (a != 10) {
System.out.println("a is not equal to 10");
} else {
System.out.println("a is equal to 10");
}
}
}
Output
a is equal to 10
a is not equal to 10
!=
operator in decision looping constructs
while (a != 0) {
System.out.printf("a : %d\n", a);
a--;
}
Find the
below working application.
NotEqualToOperatorInWhileLoop.java
package com.sample.app;
public class NotEqualToOperatorInWhileLoop {
public static void main(String[] args) {
int a = 10;
while (a != 0) {
System.out.printf("a : %d\n", a);
a--;
}
}
}
Output
a : 10
a : 9
a : 8
a : 7
a : 6
a : 5
a : 4
a : 3
a : 2
a : 1
Previous
Next
Home