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 chartype. | 
| <= | Less than or equal to | int a = 5; int b = 5; | true | Can be used with all numeric types and the chartype. | 
| > | Greater than | int a = 5; int b = 5; | false | Can be used with all numeric types and the chartype. | 
| >= | Greater than or equal to | int a = 5; int b = 5; | true | Can be used with all numeric types and the chartype. | 
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; | falsefalsefalsetrue | Will check both operands for truevalues, even if the first operand isfalse. | 
| && | Short-circuit AND | if (a && b) {..} | Same results as ANDbut if the first operand returnsfalse, the second operand will not be checked (short-circuited) andfalseis returned. | |
| | | OR | boolean a = false;boolean a = false;boolean a = true;boolean a = true; boolean b = true; | falsetruetruetrue | Will check both operands for truevalues, even if the first operand istrue. | 
| || | Short-circuit OR | if (a || b) {..} | Same results as ORbut if the first operand returnstrue, the second operand will not be checked (short-circuited) andtrueis returned. | |
| ^ | XOR(exclusiveOR) | boolean a = false;boolean a = false;boolean a = true;boolean a = true; | falsetruetruefalse | Will check both operands and return trueif they have different boolean values. | 
| ! | NOT | boolean a = false;boolean a = true; | truefalse | 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.
 
  
  
  