Скачать книгу

isAnimalAsleep = false; System.out.print(isAnimalAsleep); // false isAnimalAsleep = !isAnimalAsleep; System.out.print(isAnimalAsleep); // true

      For the exam, you also need to know about the bitwise complement operator (~), which flips all of the 0s and 1s in a number. It can only be applied to integer numeric types such as byte, short, char, int, and long. Let's try an example. For simplicity, we only show the last four bits (instead of all 32 bits).

      int value = 3; // Stored as 0011 int complement = ~value; // Stored as 1100 System.out.println(value); // 3 System.out.println(complement); // -4

      Relax! You don't need to know how to do complicated bit arithmetic on the exam, as long as you remember this rule: to find the bitwise complement of a number, multiply it by negative one and then subtract one.

      System.out.println(-1*value - 1); // -4 System.out.println(-1*complement - 1); // 3

      Moving on to more common operators, the negation operator (-) reverses the sign of a numeric expression, as shown in these statements:

      double zooTemperature = 1.21; System.out.println(zooTemperature); // 1.21 zooTemperature = -zooTemperature; System.out.println(zooTemperature); // -1.21 zooTemperature = -(-zooTemperature); System.out.println(zooTemperature); // -1.21

      Notice that in the last example we used parentheses, (), for the negation operator, -, to apply the negation twice. If we had instead written --, then it would have been interpreted as the decrement operator and printed -2.21. You will see more of that decrement operator shortly.

      int pelican = !5; // DOES NOT COMPILE boolean penguin = -true; // DOES NOT COMPILE boolean peacock = !0; // DOES NOT COMPILE

      The first statement will not compile because in Java you cannot perform a logical inversion of a numeric value. The second statement does not compile because you cannot numerically negate a boolean value; you need to use the logical inverse operator. Finally, the last statement does not compile because you cannot take the logical complement of a numeric value, nor can you assign an integer to a boolean variable.

      Tip Icon Keep an eye out for questions on the exam that use numeric values (such as 0 or 1) with boolean expressions. Unlike in some other programming languages, in Java, 1 and true are not related in any way, just as 0 and false are not related.

      Increment and Decrement Operators

      Increment and decrement operators, ++ and --, respectively, can be applied to numeric variables and have a high order of precedence compared to binary operators. In other words, they are often applied first in an expression.

Operator Example Description
Pre-increment ++w Increases the value by 1 and returns the new value
Pre-decrement --x Decreases the value by 1 and returns the new value
Post-increment y++ Increases the value by 1 and returns the original value
Post-decrement z-- Decreases the value by 1 and returns the original value

      The following code snippet illustrates this distinction:

      The first pre-increment operator updates the value for parkAttendance and outputs the new value of 1. The next post-decrement operator also updates the value of parkAttendance but outputs the value before the decrement occurs.

      Note Icon For the exam, it is critical that you know the difference between expressions like parkAttendance++ and ++parkAttendance. The increment and decrement operators will be in multiple questions, and confusion about which value is returned could cause you to lose a lot of points on the exam.

      Next, we move on to operators that take two operands, called binary operators. Binary operators are by far the most common operators in the Java language. They can be used to perform mathematical operations on variables, create logical expressions, and perform basic variable assignments. Binary operators are often combined in complex expressions with other binary operators; therefore, operator precedence is very important in evaluating expressions containing binary operators. In this section, we start with binary arithmetic operators; we expand to other binary operators in later sections.

      Arithmetic Operators

Operator Example Description
Addition a + b Adds two numeric values
Subtraction c - d Subtracts two numeric values
Multiplication e * f Multiplies two numeric values
Division

Скачать книгу