Relational Operators
COMP 1010 Home > Java Fundamentals
Introduction
Also known as comparison operators, relational operators are another set of operators that are used in the java programming language. Similar to arithmetic operators, these operators can be applied to primitive data types only. Do not attempt to use these operators with Strings!
In this section, we will be discussing relational operators such as ==, which can be used to compare primitive types of data, including int, boolean, double and char.
See page 111 in the Gaddis text: http://tinyurl.com/gaddis111
Table of Relational Operators
Operator | Meaning | Returns true if |
> |
GREATER THAN | Left operand is greater than right operand. |
>= |
GREATER THAN EQUAL TO | Left operand is greater than or equal to right operand. |
< |
LESS THAN | Left operand is less than right operand. |
<= |
LESS THAN EQUAL TO | Left operand is less than or equal to right operand. |
== |
EQUAL TO | Left operand is equal to right operand. |
!= |
NOT EQUAL TO | Left operand is not equal to right operand. |
DataType for a Relational Operator
A relational operator compares two values and determines the relationship between them. These operators can be used to test relationships of size and/or equality. Relational operators return a boolean value which is either true or false.
//greater than int a = 27, b = 45, c = 45; System.out.println (" a > b = " + (a > b)); //false System.out.println (" c > b = " + (c > b)); //false System.out.println (" b > a = " + (b > a)); //true
In the first statement, since a is not greater than b, the returned value is false. In the second statement, c is not greater tha b hence the answer is false. In the third statement, since b is greater than a, the value b > a is true.
//greater than or equal to int a = 27, b = 45, c = 45; System.out.println (" a >= b = " + (a >= b)); //false System.out.println (" b >= c = " + (b >= a)); //true System.out.println (" c >= b = " + (c >= b)); //true
In first statement, since a is not greater than or equal to b, the value is false. In the second statement ,even though b is not greater than c, it is equal to c hence we get the result as false. In the third statement, c is equal to b so we get the value as true.
//less than int a = 27, b = 45, c = 45; System.out.println (" a < b = " + (a < b)); //true System.out.println (" b < a = " + (b < a)); //false System.out.println (" c < b = " + (c < b)); //false
In first statement, since a is less than b, the value is true. In the second statement, even though b is not less than a, so the value is false. In the third statement, c is equal to b and not less than b, so we get the value as false.
//less than or equal to int a = 27, b = 45, c = 45; System.out.println (" a <= b = " + (a <= b)); //true System.out.println (" b <= a = " + (b <= a)); //false System.out.println (" c <= b = " + (c <= b)); //true
In first statement, even though a is not equal to b, it is less than a, so the answer is true. In the second statement, b is neither less than a nor equal to a, so the value is false. In the third statement, c is equal to b, so we get the value as true.
//equal to int a = 27, b = 45, c = 45; System.out.println (" a == b = " + (a == b)); //false System.out.println (" c == b = " + (c == b)); //true
In first statement, since a is not equal to b, the value is false. In the second statement, b is equal to c so the value is true.
//not equal to int a = 27, b = 45, c = 45; System.out.println ("Not equal to..."); System.out.println (" a != b = " + (a != b)); //true System.out.println (" c != b = " + (c != b)); //false
In first statement, a is not equal to b, so the result is true. In the second statement, b is equal to c so the value is false.
Summary
You should now know how to use the different relational operators. This will help you create more dynamic code that will allow for various different possibilities.
Previous Page: Arithmetic Operators | Next Page: Logical Operators |