Scala Tutorial - Scala Conditional Operators








Scala conditional operators are listed in the following Table.

OperatorOperationDescription
&&andThe values on the left and right of the operator are true. The righthand side is only evaluated if the lefthand side is true.
||orAt least one of the values on the left or right is true. The righthand side is only evaluated if the lefthand side is false.
>greater thanThe value on the left is greater than the value on the right.
>=greater than or equalsThe value on the left is greater than or equal to the value on the right.
<less thanThe value on the left is less than the value on the right.
<=less than or equals The value on the left is less than or equal to the value on the right.
==equalsThe value on the left is the same as the value on the right.
!=not equalsThe value on the left is not the same as the value on the right.




Note

&& and || are "short-circuiting" operators. They stop evaluating expressions as soon as the answer is known.

In Java, == compares object references only. It doesn't perform a logical equality check, i.e., comparing field values. You use the equals method for that purpose.

Scala uses == for logical equality, but it calls the equals method.

A new method, eq, is available when you want to compare references, but not test for logical equality.