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

guarantees left-to-right evaluation for most operators other than the ones marked in the table.

Operator Symbols and examples Evaluation
Post-unary operators expression++, expression-- Left-to-right
Pre-unary operators ++expression, --expression Left-to-right
Other unary operators -, !, ~, +, (type) Right-to-left
Cast (Type)reference Right-to-left
Multiplication/division/modulus *, /, % Left-to-right
Addition/subtraction +, - Left-to-right
Shift operators <<, >>, >>> Left-to-right
Relational operators <, >, <=, >=, instanceof Left-to-right
Equal to/not equal to ==, != Left-to-right
Logical AND & Left-to-right
Logical exclusive OR ^ Left-to-right
Logical inclusive OR | Left-to-right
Conditional AND && Left-to-right
Conditional OR || Left-to-right
Ternary operators boolean expression ? expression1 : expression2 Right-to-left
Assignment operators =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= Right-to-left
Arrow operator -> Right-to-left

      Note Icon The arrow operator (->), sometimes called the arrow function or lambda operator, is a binary operator that represents a relationship between two operands. Although we won't cover the arrow operator in this chapter, you will see it used in switch expressions in Chapter 3, “Making Decisions,” and in lambda expressions starting in Chapter 8, “Lambdas and Functional Interfaces.”

Operator Examples Description
Logical complement !a Inverts a boolean's logical value
Bitwise complement ~b Inverts all 0s and 1s 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.

      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

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