Relational & Logical OperatorsJ8 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 toint a = 5; int b = 5;
if (a == b) {..}
trueAll types can be compared for equality
!=Not Equal toint a = 5; int b = 5;
if (a != b) {..}
falseAll types can be compared for inequality
<Less thanint a = 5; int b = 5;
if (a < b) {..}
falseCan be used with all numeric types and the char type.
<=Less than or equal toint a = 5; int b = 5;
if (a <= b) {..}
trueCan be used with all numeric types and the char type.
>Greater thanint a = 5; int b = 5;
if (a > b) {..}
falseCan be used with all numeric types and the char type.
>=Greater than or equal toint a = 5; int b = 5;
if (a >= b) {..}
trueCan 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
&ANDboolean a = false;
boolean b = false;
if (a & b) {..}


boolean a = false;
boolean b = true;
if (a & b) {..}


boolean a = true;
boolean b = false;
if (a & b) {..}


boolean a = true; boolean b = true;
if (a & b) {..}


false



false



false



true
Will check both operands for true values, even if the first operand is false.
&&Short-circuit ANDif (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.
|ORboolean a = false;
boolean b = false;
if (a | b) {..}


boolean a = false;
boolean b = true;
if (a | b) {..}


boolean a = true;
boolean b = false;
if (a | b) {..}


boolean a = true; boolean b = true;
if (a | b) {..}


false



true



true



true
Will check both operands for true values, even if the first operand is true.
||Short-circuit ORif (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 b = false;
if (a ^ b) {..}


boolean a = false;
boolean b = true;
if (a ^ b) {..}


boolean a = true;
boolean b = false;
if (a ^ b) {..}


boolean a = true;
boolean b = true;
if (a ^ b) {..}


false



true



true



false
Will check both operands and return true if they have different boolean values.
!NOTboolean a = false;
if (!a) {..}

boolean a = true;
if (!a) {..}

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.