Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 588 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
588
Dung lượng
2,86 MB
Nội dung
Boolean Operators Most control flow expressions in Java direct program execution based on a computed true or false value For instance, as you'll see later in this chapter, an if (expression) statement causes the next statement to be executed only if expression evaluates to true You can actually write something like if (true), but this is not very useful in general (except for concepts such as conditional compilation, which you'll learn about later) Instead, control mechanisms such as the if statement are usually driven by a Boolean expression Note The use of the term Boolean in this chapter refers to Boolean logic in general and not the Boolean class found in Java The expressions used in flow control typically evaluate to the true and false literals associated with the boolean primitive type and not to an object reference Boolean and boolean are used somewhat interchangeably throughout this chapter depending on the context, but from a Java implementation standpoint, they are referring to the same type of data or expression Operators in Java have particular meanings when used in Boolean expressions Many of these operators are used with the other primitive types as arithmetic and bitwise operators In most cases, the Boolean meanings are a natural extension from the operations performed on integer types Table 6.1 shows the operations that can be performed on Booleans Table 6.1 Operations on Boolean Expressions Operation Name Description = Assignment As in lightOn = true; == Equality This produces a true if the two boolean operands have the same value (true or false) It produces false otherwise This is equivalent to NOT EXCLUSIVE OR (NXOR) != Inequality This produces a true if the two boolean operands have different values (one true, the other false) It produces false otherwise This is equivalent to EXCLUSIVE OR (XOR) ! Logical NOT & | ^ && || ?: A unary operator If the operand is false, the result is true, and vice versa AND Produces a true if and only if both operands are true For non-boolean operands, it is interpreted as a bitwise operator OR Produces a false if and only if both operands are false For non-boolean operands, it is interpreted as a bitwise operator XOR Produces true only if exactly one (EXCLUSIVE OR) operand is true For non-boolean operands, it is interpreted as a bitwise operator Logical Same result for boolean operands as AND described for & Logical OR Same result for boolean as described for | if-then-else Uses a boolean expression before the question mark to determine which of two expressions to evaluate The Relational Operators The most intuitive comparative operators are those that fall into a category known as relational operators Relational operators include the standard greater-than and less-than symbols you learned about back in third grade Conveniently enough, they work the same in Java as they did back in third grade, too For instance, you know that if you write (3>4), you have written something wrong (a false statement) On the other hand, (3 minimumBalance) is also a valid relational expression that provides much more potential for use in a flow control statement These expressions are built using the operators shown here: Operator < >= Boolean Result Less than Less than or equal to Greater than Greater than or equal to The precedence of the relational operators is below that of the arithmetic operators, but above that of the assignment operator Thus, the following two assignment statements produce identical results: result1 = a+b < c*d; result2 = ((a+b) < (c*d)); The associativity of relational operators is left-to-right, but this aspect really is not an issue because they can be used only with non-boolean operands It might not be immediately obvious what this implies, so consider the following (illegal) expression: a < b < c Using left-to-right associativity, the expression a < b is evaluated first to produce a boolean value of either true or false This value would then have to be compared to c What does it mean to ask if true (or false) is less than some other operand? Unlike some languages, a boolean in Java is not the same as a 0 or 1 or any other number, so a relational comparison with a boolean has no meaning An expression like this results in the compiler generating a syntax error In C and C++, the relational operators produce an integer value of 0 or 1, which can be used in any expression expecting an integer Expressions such as the following are legal in C or C++ if the variables are declared accordingly, but they generate compiler errors in Java: dailyRate = rateArray [ dayOfWeek < 4 ]; newValue = oldValue + ( newRate > oldRate ) * interest; Try a basic program to test some of what you have just learned Listing 6.1 includes some simple print statements that demonstrate the use of the relational operators with literals.Another convenient fact of Java is used here: You can concatenate boolean values with a string using the + or += operator and the string true or false will be displayed in the output Listing 6.1 QuickTest.javaA Simple Lesson from the Third Grade public class QuickTest { public static void main(String args[]){ System.out.println("5 is greater than 6:"+(5> System.out.println("6 is greater than or equa System.out.println("8 is less than 10:"+(8