Java: Using Relational and Conditional Operators - dummies

Java: Using Relational and Conditional Operators

By John Paul Mueller

Relational and conditional operators in Java attempt to ascertain the truth value — whether something is true or not — of an expression. The operation results in a Boolean output that helps an application make a decision. The most common relations are comparisons between two operands using the following operators:

  • <: Less than

  • <=: Less than or equal to

  • ==: Equal

  • >=: Greater than or equal to

  • >: Greater than

  • !=: Not equal

You’ll use these operators quite a bit as your Java experience progresses. However, you’ve probably already used them as part of a math class in determining the truth value of an expression, and you’ll find that computers use them in the same way.

Computers also require some special operators for working with data in ways that humans understand intuitively. For example, a computer needs to know whether two variables are of the same type. These concepts are a little advanced, and you don’t need to fully understand them when you begin programming.

Finally, a special conditional operator set is available. You might be familiar with conditional statements, but this is a conditional operator. The conditional operator outputs one of two values depending on the truth value of the operand expression you provide. This is one of the few ternary operators that Java uses.

Ternary operators require three operands to function properly. In the case of the conditional operator, it uses one operand to hold an expression that evaluates to true or false, a second operand that defines what to do when the expression is true, and a third operand that defines what to do when the expression is false.