Operators Flashcards
What do we need to watch out when using operators?
For usage of = in code
Which operators have the highest precedence?
Post-unary operators (expression++, expression–)
What comes after post-unary (expression++, expression–) operators in precedence?
Pre-unary operators (++expression, –expression)
What is the precedence order of other unary operators (-, !, ~, +, (type)) ?
Other unary operators come after pre-unary operators (++expression, –expression)
Where does the cast operator ((Type)reference) fit in the precedence order?
The cast operator has the same precedence as other unary operators (-, !, ~, +, (type))
What operators come after unary operators (-, !, ~, +, (type)) in precedence?
Multiplication/division/modulus operators (*, /, %)
What is the next level of precedence after multiplication/division/modulus? (*, /, %)
Addition/subtraction operators (+, -)
Where do shift operators («,»_space;,»_space;>) fall in the precedence order?
Shift operators («,»_space;,»_space;>) come after addition/subtraction (+, -)
What comes after shift operators («,»_space;,»_space;>) in precedence?
Relational operators (<, >, <=, >=, instanceof)
What is the precedence of equality operators compared to relational operators? (<, >, <=, >=, instanceof)
Equality operators (==, !=) have lower precedence than relational operators
In what order do the bitwise operators come?
After equality operators (==, !=) - Logical AND (&), then Logical XOR (^), then Logical OR (|)
Where do conditional AND (&&) and OR (||) fit in the precedence order?
Conditional AND (&&) has higher precedence than conditional OR (||), both come after bitwise operators
What is the precedence of the ternary operator?
The ternary operator (?:) comes after conditional OR and before assignment operators
Where do assignment operators (=, +=, -=, etc.) fall in the precedence order?
Assignment operators (=, +=, -=, etc.) have one of the lowest precedences
What operator has the lowest precedence?
The arrow operator (->) has the lowest precedence
What are logical operators?
AND (&), inclusive OR (^) and exclusive OR (|)
How does logical AND (&) evaluate?
true if both values are true
How does logical inclusive OR (|) evaluate?
false only if both are false
How does logical exclusive OR (^) evaluate?
true only if both are different
How do we call a part of expression that is not evaluated?
Unperformed side effect
What do we need to watch out when using logical operators?
Both values are being evaluated
What does unary operator ~ do?
Inverts 0s and 1s in literal
How do we calculate result of unary operator ~?
(n * (-1)) - 1
Casting with primitives?
Only needed if bigger literal type needs to become smaller literal type
What is overflow and underflow?
When casting bigger literal to smaller doesn’t fit
What do we need to watch out when using instanceOf?
Need to watch if we evaluate instanceOf variable that makes sense (eg. Number cannot be instance of String)
null instanceOf Object?
Always false
Numeric promotion?
Happens only when statement has numbers, and lower than int are promoted to int, and then calculated
null instanceOf null
Exception is thrown
lower primitive type + bigger primitive type = ?
bigger primitive type
whole number + float = ?
float
short + byte = ?
int
byte hat = 1;
short boots = 2 + hat;
Exception, because of the numeric promotion to int. Need to cast it back to short