Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » OperatorsRSS Feeds

Comparison Operators

Posted By: Susie Myers     Category: Java     Views: 63153

This article lists and explains comparison operators available in java.

Comparison operators are used to compare two expressions. It can be used with any type of expression type numeric and char except for "==" and "!=" operators, which can also compare references. If we compare different types variable or values, then the narrower type converted to wider type. Returned value is boolean trueor false.

Comparison Operators

 Operators

 Meaning

 <

Less than 

 <=

Less than equal to 

 > 

Greater than 

 >= 

Greater than equal to 

 == 

Double equal to 

 != 

Not equal to 



Precedence of Comparison Operators

Highest to Lowest

Operator

1

>,  >=,  <, <=

2

==, !=


Syntax of Comparison 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 Comparison Operators

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

class CompOptrDemo 
{
   public static void main(String[] args) 
   {
int a = 10, b = 15, c = 15;
System.out.println("Comparison 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

Comparison 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 Comparison Operators Or get search suggestion and latest updates.

Susie Myers
Susie Myers author of Comparison Operators is from Chicago, 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!