Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » OperatorsRSS Feeds

Relational Operators

Posted By: Roosevelt Jenkins     Category: Java     Views: 15052

This article explains about relational operators in java with examples.

Relational operator compares two values and determines the relationship between them. Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth.

Below table lists relational operators in java.

Relational Operators In Java

 Operator

Meaning 

Example 

 ==

Equals 

a == b 

 !=

Not equals 

a != b 

 >

Greater than 

a > b 

 <  

Less than 

a < b 

 >= 

Greater than or equals 

a >= b 

 <= 

Less than or equals 

a <= b 



Precedence of Relational Operators

Highest to Lowest

Operator

1

>,  >=,  <, <=

2

==, !=


Syntax of Relational Operators

op1 relOptr op2

op1 is a first operand to compare.
op2 is a second operand to compare.
relOptr is a relational operator.


Examples of Relational Operators

Example 1 : Program that displays use of all relational operators i.e ==, !=, >, <, >= and <=

class RelOptrDemo 
{
   public static void main(String[] args) 
   {
int a = 10, b = 15, c = 15;

System.out.println("Relational Operators and returned values");
System.out.println(" a > b = " + (a > b)); 
System.out.println(" a < b = " + (a < b)); 
System.out.println(" b >= a = " + (b >= a)); 
System.out.println(" b <= a = " + (b <= a)); 
System.out.println(" b == c = " + (b == c)); 
System.out.println(" b != c = " + (b != c)); 
   }
}


Output

Relational Operators and returned values
a > b = false
a < b = true
b >= a = true
b <= a = false
b == c = true
b != c = false
  
Share: 

 
 

Didn't find what you were looking for? Find more on Relational Operators Or get search suggestion and latest updates.

Roosevelt Jenkins
Roosevelt Jenkins author of Relational Operators is from New York, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!