Comparison operators enable you to compare two variables to determine whether they are equal or if one is greater than the other, and so on. But what happens when you want to check to see if a variable is in between a range of values? For example, consider validating that someone entered a correct value for an age field in your user interface. You might want to validate that the user is between the ages of 18 and 120; if someone claims to be 700 years old, you might want to check your calendar, prepare for rain, and see if anyone is building an ark! Furthermore, you might want to target a product to children and senior citizens, and therefore validate that the user's age is less than 8 or greater than 55. To address this need, Java has provided a set of logical operators that enable you to make multiple comparisons and group the result into a single boolean value. Table 3.7 lists the logical operators. ##### Table 3.7. Logical Operators Operator | Description |
---|
&& | `AND`
| || | `OR`
| #### `AND` The `AND` logical operator (&&) returns a true value if both of the variables it is comparing are true. The form of an `AND` operation is boolean1 && boolean2 It compares two Boolean variables and returns true only if both of the Boolean variables are true. For example, consider verifying that someone is of age: boolean isAdult = (age >= 18 ) && (age <= 120) If the age is 20, then 20 is greater than 18 and less than 120, so `isAdult` is `true`. If the age is 17, then 17 is not greater than or equal to 18 although it is less than 120; both conditions are not satisfied, so `isAdult` is `false`. #### `OR` The `OR` logical operator (||) returns a true value if either of the variables it is comparing are true. The form of an `OR` operation is boolean1 && boolean2 It compares two Boolean variables and returns true if either of the Boolean variables are true. For example, consider verifying that someone is either a child or a senior citizen: boolean isChildOrSenior = (age <= 10 ) || (age >= 55) If the age is 7, then 7 is less than 18, so `isChildOrSenior` is `true`. If the age is 17, then 17 is not less than or equal to 10, and it is not greater than or equal to 55; neither condition is satisfied, so `isChildOrSenior` is `false`. Not that this is an inclusive `OR`, not an exclusive `OR`; if either or both of the values are true, the result is true. #### Short-Circuit Operators One notable side effect of the logical `AND` and `OR` operators is referred to as short circuiting. This means that you are not guaranteed that both Boolean expressions will be evaluated. Consider the previous adult age verification example: boolean isAdult = (age >= 18 ) && (age <= 120) If the user's age is 10, you know that the first expression (age >= 18) is false. The `AND` operation dictates that both conditions must be true, so if the first one is false, what is the purpose of verifying the second one? If `false`, `AND` anything is always `false`, there is no point in evaluating the second expression. This Java shortcut has caught programmers off guard who attempt to perform operations in these expressions instead of simply evaluating the expression. For example, you might write a method that checks to see if a person is already in a database. The method returns true if the person is in the database or false if the user is not in the database, but it adds the person to the database if he is not already there. If for some reason this method is evaluated second in an `AND` operation and the first expression is false, this method will never be called and, hence, the person will not be added to the database. Adding the user to the database is considered a side effect of the method. Be sure to name methods exactly what they do so that you do not run into this problem. Likewise, consider the `OR` operation: boolean isChildOrSenior = (age <= 10 ) || (age >= 55) If the user is 8-years-old, the first condition is true. The `OR` operator dictates that if either or both of the conditions are true, the expression evaluates to true. Again, what is the purpose in evaluating the second expression? Be sure to understand this optimization made on behalf of the compiler and never write code that is evaluated in a logical expression that does anything except compute the value to be compared! |