2 Java Operators Flashcards

1
Q

What is the order of operator precedence?

A

Post-unary operators expression++, expression–
Pre-unary operators ++expression, –expression
Operator Symbols and examples
Other unary operators +, -, !
Multiplication/Division/Modulus *, /, %
Addition/Subtraction +, -
Shift operators <>,&raquo_space;>
Relational operators , <=, >=, instanceof
Equal to/not equal to ==, !=
Logical operators &, ^, |
Short-circuit logical operators &&, ||
Ternary operators boolean expression ? expression1 : expression2
Assignment operators =, +=, -=, *=, /=, %=, &=, ^=, !=, «=,&raquo_space;=,&raquo_space;>=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the rules of primitive numeric promotion?

A

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.

  1. 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.
  2. 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.
  3. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Draw the logical true tables for &, |, and ^

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the rules for the (short circuit) conditional operators, && and ||

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain instanceof

A
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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly