ТОП просматриваемых книг сайта:
OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн.Название OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Год выпуска 0
isbn 9781119864592
Автор произведения Jeanne Boyarsky
Жанр Программы
Издательство John Wiley & Sons Limited
TABLE 2.1 Order of operator precedence
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 |
We recommend keeping Table 2.1 handy throughout this chapter. For the exam, you need to memorize the order of precedence in this table. Note that you won't be tested on some operators, like the shift operators, although we recommend that you be aware of their existence.
->
), 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.”
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.
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