Saturday, 21 October 2017

JAVA- Operators.

Relational Operators

 Let's explore another set of useful operators available in Java known as relational operators.
Relational operators compare data types that have a defined ordering, like numbers (since numbers are either smaller or larger than other numbers).
Relational operators will always return a boolean value of true or false.
Here are a few relational operators:
  1.< : less than.
  2.<=: less than or equal to.
  3.>: greater than.
  4.>=: greater than or equal to.
  1. A relational operator is placed between the two operands(the terms that you want to compare using the relational operator). The result of a relational operation is printed out in the following statement:
System.out.println(5 < 7);
The example above will print out true because the statement "5 is less than 7" is true.

Equality Operators

You may have noticed that the relational operators did not include an operator for testing "equals to". In Java, equality operators are used to test equality.
The equality operators are:
  1. ==: equal to.
  2. !=: not equal to.
Equality operators do not require that operands share the same ordering. For example, you can test equality across booleanchar, or int data types. The example below combines assigning variables and using an equality operator:
char myChar = 'A'; int myInt = -2; System.out.println(myChar == myInt);
The example above will print out false because the value of myChar ('A') is not the same value as myInt ('-2').
Share:

2 comments: