Complement and Negation Operators – Operators – 1Z0-829 Study Guide

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 vice versa. To illustrate this, compare the outputs of the following statements:

boolean 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).

intvalue = 3;// Stored as 0011
intcomplement = ~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 oper-ator shortly.

Based on the description, it might be obvious that some operators require the variable or expression they’re acting on to be of a specific type. For example, you cannot apply a negation operator (-­) to a boolean expression, nor can you apply a logical complement operator (!) to a numeric expression. Be wary of questions on the exam that try to do this, as they cause the code to fail to compile. For example, none of the following lines of code will compile:

int pelican = !5; boolean penguin = -­true; boolean peacock = !0;

  • DOES NOT COMPILE
  • DOES NOT COMPILE
  • 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.

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.