Comparison Operators in Java : In Java like other programming languages such as C++/C, PHP, and C# you can compare the variables such integers, floats, short, long and double. All the comparison operators are binary. Our main focus in this article is the comparison of primitives data-types. If you come from C++/C background the examples most probably you already knew the content of this article.
Larger than operator - Operator (>)
This operator returns true if the first operand is larger than the second operand, false otherwise.
java code
long x=543L;
long y=243L;
if(x > y)
{
// Do thing
}
true if x larger than y else false
Larger and equal operator- Operator (>=)
This operator returns true if the first operand is larger or equal to the second operand, false otherwise.
java code
short x=3;
short y=32;
if(x >=y )
{
// do something
}
true if x larger than y or equal y else false
Less than operator- Operator (<)
This operator returns true if first operand is less than the second operand,false otherwise.
java code
float x=5.3f;
float y=55.3f;
if(x<y)
{
// Do Something
}
true if x smaller than y else false
Less than or equal operator- Operator (<=)
This operator returns true if first operand is less than or equal to the second operand,false otherwise.
java code
int x=4;
int y=5;
if(x<=y)
{
// do something
}
true if x smaller than or equal y else false
Equal Operator- Operator (==)
Check if the value of two operands are equal.
java code
int x=65;
int y=543;
if(x==y)
{
// Equal
}
true if x equal y otherwise false
Not Operator- Operator (!=)
java code
int x=4;
int y=45;
if(x!=y)
{
// Not equal
}
true if x not equal y otherwise false