Relational & Logical OperatorsS2C Home « Relational & Logical Operators
Symbols used for mathematical and logical manipulation that are recognized by the compiler are commonly known as operators in Java. In the second of five lessons on operators we look at the relational and logical operators available in Java.
Relational Operators Overview Top
Relational Operators refer to the relationships that values can have to each other. Relational Operators produce a true
or false
result and are used with control statements such as if
and while
. Any type can be compared for equality or inequality but only types that support an ordering relationship can be applied for comparison. The table below clarifies this.
Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|
== | Equal to | int a = 5; int b = 5; | true | All types can be compared for equality |
!= | Not Equal to | int a = 5; int b = 5; | false | All types can be compared for inequality |
< | Less than | int a = 5; int b = 5; | false | Can be used with all numeric types and the char type. |
<= | Less than or equal to | int a = 5; int b = 5; | true | Can be used with all numeric types and the char type. |
> | Greater than | int a = 5; int b = 5; | false | Can be used with all numeric types and the char type. |
>= | Greater than or equal to | int a = 5; int b = 5; | true | Can be used with all numeric types and the char type. |
Logical Operators Overview Top
Logical Operands must be the boolean
type and the result of a logical operation is the boolean
type and are used with control statements such as if
and while
. The following
table shows all possible combinations and their result.
Operator | Meaning | Example | Result | Notes |
---|---|---|---|---|
& | AND | boolean a = false; boolean a = false; boolean a = true; boolean a = true; boolean b = true; |
false false false true | Will check both operands for true values, even if the first operand is false . |
&& | Short-circuit AND | if (a && b) {..} | Same results as AND but if the first operand returns false , the second operand will not be checked (short-circuited) and false is returned. | |
| | OR | boolean a = false; boolean a = false; boolean a = true; boolean a = true; boolean b = true; |
false true true true | Will check both operands for true values, even if the first operand is true . |
|| | Short-circuit OR | if (a || b) {..} | Same results as OR but if the first operand returns true , the second operand will not be checked (short-circuited) and true is returned. | |
^ | XOR (exclusive OR ) | boolean a = false; boolean a = false; boolean a = true; boolean a = true; |
false true true false | Will check both operands and return true if they have different boolean values. |
! | NOT | boolean a = false; boolean a = true; |
true false | Will check if operand is not true . |
The short-circuit operators &&
and ||
can be more efficient to use; if you want both operands to be evaluated use the &
and |
operators.
Related Quiz
Fundamentals Quiz 7 - Relational & Logical Operator Quiz
Lesson 8 Complete
In this lesson we looked at the relational & logical operators used in Java.
What's Next?
In the next lesson we look at the assignment operators used in Java.