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 […]
Adding Parentheses You might have noticed we said “Unless overridden with parentheses” prior to presenting Table 2.1 on operator precedence. That’s because you can change the order of operation explicitly by wrapping parentheses around the sections you want evaluated first. Changing the Order of Operation Let’s return to the previous price example. The following code […]
Increment and Decrement Operators Increment and decrement operators, ++ and –, respectively, can be applied to numeric var-iables and have a high order of precedence compared to binary operators. In other words, they are often applied first in an expression. Increment and decrement operators require special care because the order in which they are attached […]
Complement and Negation Operators Since we’re going to be working with a lot of numeric operators in this chapter, let’s get the boolean one out of the way first. The logical complement operator (!) flips the value of a boolean expression. For example, if the value is true, it will be converted to false, and […]
Operator Precedence When reading a book or a newspaper, some written languages are evaluated from left to right, while some are evaluated from right to left. In mathematics, certain operators can override other operators and be evaluated first. Determining which operators are evaluated in what order is referred to as operator precedence. In this manner, […]
OCP EXAM OBJECTIVES COVERED IN THIS CHAPTER: ✓✓ Handling date, time, text, numeric and boolean values parentheses, type promotion, and casting to evaluate arithmetic and boolean expressions The previous chapter talked a lot about defining variables, but what can you do with a variable once it is created? This chapter introduces operators and shows how […]
var blocky = “”” squirrel \s pigeon \ termite”””; System.out.print(blocky); var num1 = Long.parseLong(“100”); var num2 = Long.valueOf(“100”); System.out.println(Long.max(num1, num2)); public class Price { public void admission() { INSERT CODE HERE System.out.print(amount); } }