Java Operator Precedence Flashcards
What’s of the top (15th) precedence?
() - Parentheses
[] - Array subscript
· - Member selection
What’s of the last (1st) precedence?
= -Assignment \+= -Addition Assignment -= -Subtraction Assignment *= -Multiplication Assignment /= -Division Assignment %= -Modulus Assignment
What does Precedence even mean?
it’s the condition of being more important that something; priority
in this case, the higher the #, the higher it is in priority
14th precedence?
? : -conditional (ternary)
13th precedence?
\++ -unary Pre-Increment -- -unary Pre-Decrement \+ -unary Plus - -unary Minus ! -unary Logical Negation ~ -unary Bitwise Complement (type) -unary Type Cast
12th precedence?
- -Multiplication
/ -Division
% -Modulus
11th precedence?
+ -Addition
- -Subtraction
10th precedence?
«_space; -bitwise Left Shift
» -bitwise Right Shift w/Sign Extension
»> -bitwise Right Shift w/Zero Extension
9th precedence?
< -relational Less than <= -relational Less than or Equal > -relational Greater than >= -relational Greater than or Equal instanceof -Type comparison (objects only)
8th precedence?
== -relational Is Equal to != -relational Is Not Equal to
7th precedence?
& -bitwise AND
6th precedence?
^ -bitwise exclusive OR
5th precedence?
-bitwise inclusive OR
4th precedence?
&& -logical AND
3rd precedence?
|| -logical OR
What’s the order for Bitwise operators?
(10th precedence)
«_space; -bitwise Left Shift
» -bitwise Right Shift w/Sign Extension
»> -bitwise Right Shift w/Zero Extension
(7th precedence)
& -bitwise AND
(6th precedence)
^ -bitwise exclusive OR
(5th precedence)
| -bitwise inclusive OR
What’s the order for Relational operators?
(9th precedence) < -relational Less than <= -relational Less than or Equal > -relational Greater than >= -relational Greater than or Equal instanceof -Type comparison (objects only)
(8th precedence)
== -relational Is Equal to
!= -relational Is Not Equal to
What’s the order for Unary operators?
(14th precedence)
++ -unary Post-Increment
– -unary Post-Decrement
(13th precedence) \++ -unary Pre-Increment -- -unary Pre-Decrement \+ -unary Plus - -unary Minus ! -unary Logical Negation ~ -unary Bitwise Complement (type) -unary Type Cast
What’s the order for Math operators?
(15th precedence)
() - Parentheses
[] - Array subscript
· - Member selection
(12th precedence)
* -Multiplication
/ -Division
% -Modulus
(11th precedence)
+ -Addition
- -Subtraction
(Check for understanding Math) Which one comes first?
- Multiplication
- Division
- Modulus
None of them
It depends on the order from left to right
-b/c they are all within the same precedence, just as + and - have the same precedence as well.
What’s the order for Boolean operators?
(13th precedence)
! -unary Logical Negation
(7th precedence)
& -bitwise AND
(6th precedence)
^ -bitwise exclusive OR
(4th precedence)
&& -logical AND
(3rd precedence)
|| -logical OR
What makes the 14th, 13th, 2nd, and 1st precedence different from the rest?
Within Java, they operate going from Right to Left instead of Left to Right
This is what we call “Associativity”, whenever we have more than one expression with the same precedence, the order can change.
Ex) “3 + 3 + 3” is Left to Right (13th precedence)
“3 = 3 = 3” is Right to Left (2nd precedence)
What is a Bitwise operator used for?
They’re used to manipulate r compare individual bits of a number
(works with char, short, int, etc.)
In use, it works like Booleans, except every 1 = true and 0 = false
What is a Unary operator used for?
“Unary” means to have one operands
These operands can be increment (++), decrement (–), negation(!)
Increment and Decrement can be used in a Pre- or a Post- state