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); } }
public void printMagicData() { var magic = ; System.out.println(magic); } public void print(Water water) { System.out.println(water); } } package aquarium; public class Water { boolean salty = false; } package aquarium.jellies; public class Water { boolean salty = true; } package employee; INSERT IMPORTS HERE public class WaterFiller { Water water; } import aquarium.jellies.Water; import […]
Review Questions The answers to the chapter review questions can be found in the Appendix. public class Bunny { public static void main(String[] x) { Bunny bun = new Bunny(); } } public class KitchenSink { private int numForks; public static void main(String[] args) { int numKnives; System.out.print(“”” “# forks = ” + numForks + […]
Exam Essentials Be able to write code using a main() method. A main() method is usually written as public static void main(String[] args). Arguments are referenced starting with args[0]. Accessing an argument that wasn’t passed in will cause the code to throw an exception. Understand the effect of using packages and imports. Packages contain Java […]