Java Reference
In-Depth Information
then charge a reduced bus fare. Then you require both conditions—a person must be over 60 AND
they must have a bus card—for the entire statement to be true. Regardless of whether a person is 50
years old with a bus card or if they are 62 years old without a bus card, their fare will not be reduced.
& and | versus && and
This section covered && and , the AND and OR operators. There are also bit-
wise operators called & and |, which behave similarly on Boolean operands. The
main difference is that the double symbols && and will first check the left side
operand and will only check the right side if necessary. So for &&, if the left side
is true, it will check if the right side is also true. If the left side is false, then you
already know the outcome will be false and the right side will not be evaluated.
This is called short circuiting, because like electricity will take the shortest path
(sometimes causing a short circuit if there is another shorter route), these operators
will stop the evaluation early if the answer is already known. This is particularly
useful in avoiding exceptions or errors, when the first operator must be true in
order to evaluate the second operator. A common example is (x != 0 && 1/x > 1). If
you use & here, and x equals 0, then 1/x will be evaluated, but of course, zero can-
not be a divisor so this would cause an error. Java's inclusion of the && operator
with short circuiting will prevent this kind of error.
Similarly, for , if the left side is true, there is no need to check if the right side is
true, so the result will be true without evaluating the right operand. If, however,
the left operand is false, it will check the right operand. You could use a very simi-
lar example (x == 0 1/x < 1) to see how the short circuiting can prevent the same
errors when using the OR operand.
comparing composite data types with
comparison methods
When you think of the differences between primitive data types, like int , and composite date types,
like String , it should not be surprising that you will need to compare them in different ways. If
asked the question, “Is 5 less than 10?” almost everyone will compare the two numbers in the same
way and respond affirmatively. Although the comparison of char variables is not as immediately
apparent, all possible char values have been assigned a numeric value, allowing them to be ordered
similarly to integers. However, the question, “Is order less than delivery?” is not at all apparent.
Therefore, comparison methods for composite data types must be defined in the class, rather than
using the comparison operators discussed in the previous section.
This early chapter, in addition to the primitive types, includes a discussion on strings and arrays, since
you will encounter both these composite data types frequently. Many more classes will be covered in
later chapters. These classes are well-defined and include comparison methods. It often makes sense
to compare strings relationally, such as putting a list into alphabetical order. For other composite data
types, including pre-existing classes and classes you will create on your own, comparison methods can
be implemented in different ways. You will read about some of these possibilities later in the topic.
 
 
Search WWH ::




Custom Search