Summary This chapter covered a wide variety of Java operator topics for unary, binary, and ternary operators. Hopefully, most of these operators were review for you. If not, you need to study them in detail. It is important that you understand how to use all of the required Java oper-ators covered in this chapter and […]
The final operator you should be familiar with for the exam is the conditional operator, The first operand must be a boolean expression, and the second and third operands can be any expression that returns a value. The ternary operation is really a condensed form of a combined if and else statement that returns a […]
Conditional Operators Next, we present the conditional operators, && and ||, in Table 2.10. TABLE 2 . 10 Conditional operators Operator Example Description Conditional a && b Value is true only if both values are true. If the left side is false, then AND the right side will not be evaluated. Conditional c || d […]
Invalid instanceof One area the exam might try to trip you up on is using instanceof with incompatible types. For example, Number cannot possibly hold a String value, so the following causes a compilation error: public void openZoo(Number time) { if(time instanceof String) // DOES NOT COMPILE System.out.print(time); } If the compiler can determine that […]
Relational Operators We now move on to relational operators, which compare two expressions and return a boolean value. Table 2.8 describes the relational operators you need to know for the exam. TABLE 2.8 Relational operators Operator Example Description Less than a < 5 Returns true if the value on the left is strictly less than […]
Return Value of Assignment Operators One final thing to know about assignment operators is that the result of an assignment is an expression in and of itself equal to the value of the assignment. For example, the following snippet of code is perfectly valid, if a little odd-looking: long wolf = 5; long coyote = […]
Casting Values vs. Variables Revisiting our third numeric promotional rule, the compiler doesn’t require casting when working with literal values that fit into the data type. Consider these examples: byte hat = 1; byte gloves = 7 * 10; short scarf = 5; short boots = 2 + 1; All of these statements compile without […]
Reviewing Primitive Assignments See if you can figure out why each of the following lines does not compile: int fish = 1.0; // DOES NOT COMPILE short bird = 1921222; // DOES NOT COMPILE int mammal = 9f; // DOES NOT COMPILE long reptile = 192_301_398_193_810_323; // DOES NOT COMPILE The first statement does not […]
Assigning Values Compilation errors from assignment operators are often overlooked on the exam, in part because of how subtle these errors can be. To be successful with the assignment operators, you should be fluent in understanding how the compiler handles numeric promotion and when casting is required. Being able to spot these issues is critical […]
Numeric Promotion Now that you understand the basics of arithmetic operators, it is vital to talk about prim-itive numeric promotion, as Java may do things that seem unusual to you at first. As we showed in Chapter 1, “Building Blocks,” each primitive numeric type has a bit-length. You don’t need to know the exact size […]