Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 133
Applying Unary Operators
ОглавлениеBy definition, a unary operator is one that requires exactly one operand, or variable, to function. As shown in Table 2.2, they often perform simple tasks, such as increasing a numeric variable by one or negating a boolean
value.
TABLE 2.2 Unary operators
Operator | Examples | Description |
---|---|---|
Logical complement | !a | Inverts a boolean 's logical value |
Bitwise complement | ~b | Inverts all 0 s and 1 s in a number |
Plus | +c | Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator |
Negation or minus | -d | Indicates a literal number is negative or negates an expression |
Increment | ++e f++ | Increments a value by 1 |
Decrement | --f h-- | Decrements a value by 1 |
Cast | (String)i | Casts a value to a specific type |
Even though Table 2.2 includes the casting operator, we postpone discussing casting until the “Assigning Values” section later in this chapter, since that is where it is commonly used.