Relational Operators in Java
Relation operators are used to compare two quantities depending on their relation and depending upon relation it takes certain decision. The relational operators determine the relationship that one operand has with other operand. The expression used relational operator to control the if statement and the various loop statements.
Operator | Description | Example | Explanation |
---|---|---|---|
== | Evaluates whether the operands are equal. | x==y | Returns true if the value are equal otherwise false. |
!= | Evaluates whether the operands are not equal. | x!=y | Returns true if value are not equal and false otherwise. |
> | Evaluates whether the left operand is greater than the right operand. | x>y | it is true if x is greater than y and false otherwise. |
< | Evaluates whether the Right operand is greater than the left operand. | x<y | Returns true if x is less than y and false otherwise. |
>= | Evaluates whether the left operand is greater than or equal to the right operand. | x>=y | Returns true if x is greater than or equal to y and false otherwise. |
<= | Evaluates whether the Right operand is greater than the left operand. | x<=y | Returns true if x is less than or equal to y and false otherwise. |
Logical Operators in Java
logical operators are used when we want to form compound conditions by combing two or more relations
Operator | Meaning | Description | Example | Explanation |
---|---|---|---|---|
&& | Logical AND | Evaluates true if both the conditions are true and false otherwise | x=6,y=7 x>5&&y<10 | The result is true if condition1(x>5)and condition2(y<10)are both true.if one of them is false,resultof condition will be false, x=6,y=7 hence condition is true in this case |
|| | Logical Or | Evaluates true if at least one of the conditions are true and false otherwise if both the condition are false | x=5,y=11 x>5||y<10 | The result is true if any one i.e condition1 (x>5)or condition2 (y<10)is true.if one of x=6,y=7condition1 holds true hence condition is true in this case |
! | Logical NOT | It inverts the result | x=true y=!x | The value of x=true initially. y=!x stores false |
Java Program for Relational Operator
import java.lang.*;
import java.util.*;
public class Greater
{
public static void main(String args[])
{
int a=100 ,b=20,c=50;
if(a>b && a>c)
System.out.println("A is Greater");
else if(b>a && b>c)
System.out.println("B is Greater");
else
System.out.println("B is Greater");
}
}
Output:
A is Greater
Types of Question can be asked in exam
- What is relational and logical operators in java explain with java example?
- Explain relational operators in java?
- Explain logical operators in java?
- Write a java program to demonstrate the use of relational and logical operators in java?
- Explain type of relational and Logical operators