Chapter 2 Operators And Statements Flashcards
Using operators and decision constructs Using loop Constructs
A Java operator is
a special symbol that can be applied to a set of variables, values or
literals—referred to as operands—and that returns a result.
Name the three flavors of operators in Java
unary
binary
ternary
Java operators follow
order of operation, by decreasing order of operator precedence
Java guarantees left-to-right evaluation if
two operators have the same level of precedence
What type of operators: expression++, expression–
Post-unary operators
What type of operators: ++expression, –expression
Pre-unary operators
What type of operators: +, -, !
Other unary operators
What type of operators: *, /, %
Multiplication/Division/Modulus
What type of operators: +, -
Addition/Subtraction
What type of operators: <>,»_space;>
Shift operators
What type of operators: , <=, >=, instanceof
Relational operators
What type of operators: ==, !=
Equal to/not equal to
What type of operators: &, ^, |
Logical operators
What type of operators: &&, ||
Short-circuit logical operators
What type of operators: boolean expression ? expression1 : expression2
Ternary operators
What type of operators: =, +=, -=, *=, /=, %=, &=, ^=, !=, «=,»_space;=,»_space;>=
Assignment operators
binary operators can be used to
perform mathematical operations on variables,
create logical expressions, as well as perform basic variable assignments.
Can you use modulus operations on negative integers and floating point integers?
Yes
Numeric promotion
- 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.
What is the data type of x * y?
int x = 1;
long y = 33;
If we follow the fi rst rule, since one of the values is long and the other is int, and long is larger than int, then the int value is promoted to a long, and the resulting value is long.
What is the data type of x + y?
double x = 39.21;
float y = 2.1;
This is actually a trick question, as this code will not compile! Floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f. If the value was set properly to 2.1f, then the promotion would be
similar to the last example, with both operands being promoted to a double, and the result would be a double value.
What is the data type of x / y?
short x = 10;
short y = 3;
In this case, we must apply the third rule, namely that x and y will both be promoted
to int before the operation, resulting in an output of type int. Pay close attention to
the fact that the resulting output is not a short
What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;
First, x will automatically be promoted to
int solely because it is a short and it is being used in an arithmetic binary operation. The promoted x value will then be automatically promoted to a float so that it can be multiplied with y. The result of x * y will then be automatically promoted to a double, so that it can be multiplied with z, resulting in a double value.
By definition, a unary operator is one that requires
exactly one operand, or variable, to function.
[Blank} Unary operator
Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator
+
[Blank] Unary operator
Indicates a literal number is negative or negates an expression
-
[Blank] Unary operator
Increments a value by 1
++