Comparison Operators in java

Comparison Operators

Relational operators in Java are used to compare 2 or more objects. Java provides six relational operators:greater than (>), less than (<), greater than or equal (>=), less than or equal (<=), equal (==), and not equal (!=).

All relational operators are binary operators, and their operands are numeric expressions.
Binary numeric promotion is applied to the operands of these operators. The evaluation results in a boolean value. Relational operators have precedence lower than arithmetic operators, but higher than that of the assignment operators. An example program is shown below that demonstrates the different relational operators in java.
Ex:

public class RelationalOperatorsDemo {
 public RelationalOperatorsDemo( ) {
 int x = 10, y = 5;
 System.out.println(”x > y : “+(x > y));
 System.out.println(”x < y : “+(x < y));
 System.out.println(”x >= y : “+(x >= y));
 System.out.println(”x <= y : “+(x <= y));
 System.out.println(”x == y : “+(x == y));
 System.out.println(”x != y : “+(x != y));
 }

 public static void main(String args[]){
 new RelationalOperatorsDemo();
 }
 }
Logical operators return a true or false value based on the state of the Variables. There are six logical, or boolean, operators. They are AND, conditional AND, OR, conditional OR, exclusive OR, and NOT. Each argument to a logical operator must be a boolean data type, and the result is always a boolean data type. An example program is shown below that demonstrates the different Logical operators in java.

Ex:
public class LogicalOperatorsDemo {
public LogicalOperatorsDemo() {
boolean x = true;
boolean y = false;
System.out.println("x & y : " + (x & y));
System.out.println("x && y : " + (x && y));
System.out.println("x | y : " + (x | y));
System.out.println("x || y: " + (x || y));
System.out.println("x ^ y : " + (x ^ y));
System.out.println("!x : " + (!x));
}

public static void main(String args[]) {
new LogicalOperatorsDemo();
}
}




Share this

Related Posts

Previous
Next Post »

Recent