Java supports different types of operators. Here the Relational operators and the Logical operators are explained using Java Codes and exercises.
Java Operators: Article Quizzes |
1- Relational Operators
Relational operators are binary operators, and their operands are numeric expressions. The result of the evaluation is either true or false.
Example is: if ( i == 20 ) this asks “does i equal to 20?” if it is true, the statement is executed. The statement is ignored if i is not equal to 20, because the result is false.
The relational operators in Java are:
== | Equal |
!= | Not equal |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
Exercise 1
What is written to the standard output as the result of executing the following statements?
class RelationalOperator { public static void main(String[] args){ int i = 8; if(i != 7) { System.out.print("X"); } else { System.out.print("Y"); } } }
Exercise 2
What is written to the standard output as the result of executing the following statements?
class RationalOperator { public static void main(String[] args){ int i = 100; if(i <= 30) { System.out.print("M"); } else { System.out.print("O"); } } }
Exercise 3
What is written to the standard output as the result of executing the following statements?
class RelationalOperator { public static void main(String[] args){ int i = 10; int i2 = 20; int i3 = 30; if(i3 <= 30) { System.out.print("K"); } if(i == i3) { System.out.print("L"); } if(i3 >= 22) { System.out.print("M"); } if(i2 > 18) { System.out.print("N"); } if(i != 10) { System.out.print("O"); } } }
2- Conditional Operators
The conditional operators in Java ( &&, || ) are used in conditional statements such as if and while statements. The following example shows the difference between “&&” and “||”
int i = 4; int r = 6;
if(i == 4 && r ==3 ) // By using “&&” the result is false, because r is not equal to 3. One false results in false.
if(i ==4 || r ==3) // By using “||” the result is true, because i is equal to 4. One true results in true.
&& | AND |
|| | OR |
Exercise 4
What is written to the standard output as the result of executing the following statements?
class LogicalOperator { public static void main(String[] args){ int i = 10; if(i > 9 && i != 10) { System.out.print("P"); } else { System.out.print("Q"); } } }
Exercise 5
What is written to the standard output as the result of executing the following statements?
class LogicalOperator { public static void main(String[] args){ int i = 10; if(i != 9 || i > 20) { System.out.print("W"); } else { System.out.print("Z"); } } }
Exercise 6
What is written to the standard output as the result of executing the following statements?
class LogicalOperator { public static void main(String[] args){ int i = 10; int i2 = 20; int i3 = 30; if(i3 == 30 && i2 > 20) { System.out.print("R"); } if(i != i3 || i2 == 21) { System.out.print("S"); } if(i3 >= 22 && i != 11) { System.out.print("T"); } if(i2 > 18 || i == 11) { System.out.print("U"); } } }Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.
Java Operators: Article Quizzes |
Suggested Articles
Books by Sar Maroof |
![]() ![]() ![]() ![]() |
![]() ![]() ![]() |
The accident of finding this post has my day
Nice article and easy to understand!
Created the greatest artcis, you have.