2 Java Operators Flashcards
What is the order of operator precedence?
Post-unary operators expression++, expression–
Pre-unary operators ++expression, –expression
Operator Symbols and examples
Other unary operators +, -, !
Multiplication/Division/Modulus *, /, %
Addition/Subtraction +, -
Shift operators <>,»_space;>
Relational operators , <=, >=, instanceof
Equal to/not equal to ==, !=
Logical operators &, ^, |
Short-circuit logical operators &&, ||
Ternary operators boolean expression ? expression1 : expression2
Assignment operators =, +=, -=, *=, /=, %=, &=, ^=, !=, «=,»_space;=,»_space;>=
What are the rules of primitive numeric promotion?
Numeric Promotion Rules
1. If two values have different data types, Java will automatically promote one of the values
to the larger of the two data types.
- If one of the values is integral and the other is floating-point, Java will automatically
promote the integral value to the floating-point value’s data type. - Smaller data types, namely byte, short, and char, are first promoted to int any time
they’re used with a Java binary arithmetic operator, even if neither of the operands is int. - After all promotion has occurred and the operands have the same data type, the resulting
value will have the same data type as its promoted operands.
Draw the logical true tables for &, |, and ^
Here are some tips to help remember this table:
■ AND is only true if both operands are true.
■ Inclusive OR is only false if both operands are false.
■ Exclusive OR is only true if the operands are different.
Explain the rules for the (short circuit) conditional operators, && and ||
The short-circuit operators are nearly identical to the logical operators,
& and |, respectively, except that the right-hand side of the expression may never be
evaluated if the fi nal result can be determined by the left-hand side of the expression.
Explain instanceof
Will return T or F. Left hand side must be an object, not a primitive. This will not compile: int s =1; Boolean b = s instanceof Number;