Operators & Statements Flashcards
Is the following assignment valid? short = short + short;
No. short is always promoted to int before being used in an arithmetic binary operation, which cannot be assigned back to short.
Is the following assignment valid? int = byte + short;
Yes. byte and short are automatically promoted to int before added.
Is the following assignment valid? short = short++;
Yes. Unary operators don’t promote shorts.
Is the following assignment valid? float f = 4.3;
No. floating-point literals are assumed to be doubles unless postfixed with ‘f’, which cannot be assigned to float.
Is the following assignment valid? double = short + float;
Yes. short is promoted to int automatically, then to float to be added, then the result is promoted to double to be assigned.
Is the following assignment valid? boolean b = !0;
No. The negation operator is not valid on a numeric value.
Is the following assignment valid? int i = 5.0;
No. 5.0 is treated as a double, which cannot be assigned to an int unless casted.
Is the following assignment valid? short s = 35000;
No. 35000 exceeds the maximum value of short and can’t be assigned unless casted, and then overflowing would apply.
Is the following assignment valid? long l = 35000;
Yes.
Is the following assignment valid? long l = 2500000000;
No. 2500000000 exceeds the maximum value of an int. An ‘L’ must follow the literal value to be treated as a long literal.
Is the following assignment valid? boolean b = int == null;
No. null cannot be compared to primitives
Is the following assignment valid? boolean b = Boolean == null;
Yes.
Given int x = 2;, What is the value of the following expression?
x++ + (2 + –x) * ++x;
- The unary operators are evaluated left to right regardless of parentheses, which simplifies to:
2 + (2 + 2) * 3;
Which of the following types is not supported by switch statements? byte/Byte, short/Short, char/Character, int/Integer, long/Long, enum, String.
long/Long (enum support added in Java 5, String in Java 7)
What is a Java operator?
It is a special symbol that can be applied to a set of variables, values, or literals that returns a result.
What is an operand in the context of an operator in Java?
An operand is one of set of variables, values, or literals, which can be used to return a result from an operator.
What are the 3 flavors of Java operators?
Unary, binary, and ternary.
How many operands can a unary operator be applied to?
1
How many operands can a binary operator be applied to?
2
How many operands can a ternary operator be applied to?
3
What is the order of operator precedence in Java?
Post-unary operators Pre-unary operators Other unary operators Multiplication/Division/Modulus Addition/Subtraction Shift operators Relational operators Equal to/not equal to Logical operators Short-circuit logical operators Ternary operators Assignment operators
When does Java not follow the rules of operator precedence?
When the order of operation is overridden with parentheses.
If all operators are equal, how does Java evaluate an expression?
From left to right.
What are the arithmetic operator?
+, -, * /, %, ++, –
Which primitives are operands for arithmetic operators?
All except boolean.
Note: A String is not a primitive though some texts treat it that way. The arithmetic + and += perform concatenation on strings, not arithmetic.
What is one of the rules of numeric promotion in Java?
If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
What is another rules of numeric promotion in Java?
If one of the value is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value’s data type.
What is yet another rule of numeric promotion in Java?
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.
What is the other rule of numeric promotion in Java?
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.
What does the ‘+’ unary operator do?
Indicates that a number is positive (although numbers in Java are assumed to be positive unless accompanied by a negative unary operator.)
What does the ‘-‘ unary operator do? What is it called?
It indicates a literal number is negative or negates an expression. It is called the negation operator.
What does the ‘++’ operator do?
Increments a value by 1.
What does the ‘–’ operator do?
Decrements a value by 1.
What does the ‘!’ operator do? What is it called?
It inverts a Boolean’s logical function. It is called the logical complement operator.
Can you apply the logical complement operator to an arithmetic expression?
No.
Can you apply the negation operator to a boolean expression?
No.
What is the ‘++’ operator called?
The increment operator.
What is the ‘–’ operator called?
The decrement operator.
What is it called if the increment operator or decrement operator is placed before and operand?
The pre-increment or pre-decrement operator.
What is it called if the increment operator or decrement operator is placed after an operand?
The post-increment or post-decrement operator.
Which operator has higher precedence in the order of operations, the pre-increment/pre-decrement operators or the post-increment/post-decrement operators?
The pre-increment/pre-decrement operators.