How to use the relational operator in java programming


With in this java programming lesson you'll learn to use relational and equality operators to compare data. check out the java program below. it checks whether 20 is less than 50 and prints the result.

public class Main{
public static void main(String[] args){
System.out.println(20<50); //boolean value true
System.out.println(20>50);  //boolean value false
System.out.println(20==50); // equality operator
System.out.println(20!=50); // not equal operator
System.out.println(20<=50); //less than or equal operator
System.out.println(20>=50); // greater than or equal operator
System.out.println(560+634<953-795); // mix statement
System.out.println("hello".equals("hello")); // String comparison using "equals" Method
  System.out.println("Hello".equals("hi"));//String comparison using .equals method
}
}

 The result of the comparison is a boolean value which is either "true" or "false". Now compare whether 20 is greater than 50 and print the result or see the codes above. The result would be false in the second comparison, now let's proceed to our next exercise. Compare whether 20 is equal to 50 and print the result, use the equality operator (==), as expected the result will be false, Now compare whether 20 is not equal to 50 use inequality operator or(!=) and print the result. Compare whether 20 is less than or equal to 50, use the (<=)operator, and print the result. Now compare whether 20 is greater than or equal to 50  use the (>=) operator. as Expected the print result will be false, now you already know the Equality and Relational Operators. you are now ready to the next level, let us try to Mix it. Compare whether 560 plus(+) 634 is less than 953 minutes (-) 795 then print the result, look at the code above for your reference. Beautiful! you've learned how to compare numbers in java. It is also possible to compare Strings. Check out the program I've added above which checks the equality of two strings. instead of an operator, it uses the "equals" method. We will tackle the methods in detail later. which is "true" at the same time. Use the "equals" method to compare whether The string "Hello" is Equal to "hi" see the codes above. The print result would be "false" as expected, Congrats you've finished the relational operators' lesson.

Post a Comment

0 Comments