Relational Operator in Java is used where we use any comparison between two value or two variable or a value and a variable, but while comparing variables , it must some numeral value and hence all the operators used for comparison called as comparison operators.
On the other side there are some operator used for comparing equality are called as equality operator.
So finally Relational operator compromise of two operators i.e Comparison operator and Equality operator.
Comparison Operator, its types and uses
As said above its only use is to compare either direct values or through variables.It is generally used with the control statement like if , while and do-while and with conditional operator as well.
Why it is used with only these control statement ?
--> It is because these control statement on the condition whether it is true or false and this operator return true or false based on the condition check.
There are four types of Comparison Operators. they are:-
1> Greater than operator ( > ) :- It is used to compare whether the 1st operand is greater than the 2nd operand or not.
for ex -
if ( 20 > 10 ) // it will return true because the condition is true.
while ( 20 > 10 ) // Here condition is true and loop will run infinite times to avoid we can
use break statement inside the loop.
int x = 20 ;
int y = 10 ;
if ( x > y )
Here we are using variables for comparison. It will work fine and compiler and run successfully.
1> Greater than and Equal to operator ( >= ) :- It is used to compare whether the 1st operand is greater than or Equal to the 2nd operand or not.Only difference between " > " and " >= " is that with the >= . We can be able to compare for the equality of the value as well.
for ex- You can prefer the above example simply change the operator and see the difference.
1> Less than operator ( < ) :- It is used to compare whether the 1st operand is less than the 2nd operand or not.
for ex -
if ( 20 < 10 ) // It will return false because here the condition is false.
while ( 20 < 10 ) // Here condition is false and loop will not run at all .
int x = 20 ;
int y = 10 ;
if ( x < y )
Here we are using variables for comparison. It will work fine and compiler and run successfully.
1> Less than and Equal to operator ( <= ) :- It is used to compare whether the 1st operand is less than or Equal to the 2nd operand or not.Only difference between " < " and " <= " is that with the <= we can be able to compare for the equality of the value as well.
for ex- You can prefer the above example simply change the operator and see the difference.
Note:- You can use directly all these operator in the print statement to check whether it is returning true or false.
for ex-
System.out.println(20>10); //It will print true because the condition defined is true here.
System.out.println(20<10); //It will print false because the condition defined is false here.
Equality Operator, its types and uses
Equality operators are used to check whether the value is equal or not. But we can't be able to compare two strings with the help of equality operator because String is a class in java so when ever we are defining any string compiler will create the reference for that string and then reference value is stored in the String variable. And if we are comparing those variable then internally it is comparing the reference variable not the assigned string to that variable.Details of which we will see in String topic later in this tutorial.
Note :- In java we have some predefined functions here " equals() " is used for string comparison.
There are two types of quality operator in Java :-
1> Equal to operator ( == ) :- As said above it only checks whether two values or variables are " equal " or not and it will return true or false according to the condition check.
for ex -
if ( 20 == 10 ) // It will return false because the condition is false.
String s = " Java " ;
String s1 = " Java " ;
System.out.println ( " Comparison result : " + ( s == s1 ) ) ;
It will compile successfully but it will display true , you must be wondering that you above i said it will not compare the string but here it is giving true, but my friend it is not true because of "String value" but because of reference value which is stored in " s " and " s1 ".
I am sure you again wondering how reference will be same if we are defining two different string type variable. It is because compiler will check whether the String value is same or not , if it is same then it will store the same reference value in " s1 " as in " s " .
Note :- In the above code if you create object of the string type then it will return false whether the string defined is same or not because by creating object both the reference variable s and s1 contain different reference value because its a matter of object and we are creating two object because every object has it own memory and own property.Like this,
String s = new String("Java") ;
String s1 = new String("Java") ;
System.out.println ( " Comparison result : " + ( s == s1 ) ) ;
If we compiler and run the above code it will return false. Reason as said above as a note.
2>Not Equal to operator ( != ) :- It checks whether the two value or variable is " not equal " or not and it will return true or false according to the condition check.
for ex -
if ( 20 != 10 ) // It will return false because the condition is false.
ReplyDeletenice article for beginners.thank you.
java operators
java operators