Chapter 2 - Operators and Statements Flashcards
Operator Precendence
(12)
Post-unary operators: expression++, expression–
Pre-unary operators: ++expression, –expression
Other unary operators : +, -, !
Multiplication/Division/Modulus : *, /, %
Addition/Subtraction: +, -
Shift operators : <<, >>, >>>
Relational operators: <, >, <=, >=, instanceof
Equal to/not equal to: ==, !=
Logical operators: &, ^, |
Short-circuit logical operators : &&, ||
Ternary operators : boolean expression ? e1 : e2
Assignment operators : =, +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>=
Numeric Promotion
(4)
- 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.
Unary Operators
(5)
- Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator
- Indicates a literal number is negative or negates an expression
- ++ Increments a value by 1
- – Decrements a value by 1
- ! Inverts a Boolean’s logical value
Compound Assignment Operators
long x = 10;
int y = 5;
y = y * x; –> does not compile
y *= x; –> compiles
Equality Operators
(3)
- Comparing two numeric primitive types. If the numeric values are of different data types, the values are automatically promoted as previously described. For example, 5 == 5.00 returns true since the left side is promoted to a double.
- Comparing two boolean values.
- Comparing two objects, including null and String values.
Ternary Operator
(2)
- booleanExpression ? expression1 : expression2
- As of Java 7, only one of the right-hand expressions of the ternary operator will be evalu- ated at runtime.
Switch Statement
(6)
Data types supported by switch statements include the following: (no long or boolean)
- byte and Byte
- short and Short
- char and Character
- int and Integer String
- enum values
- only compiletime Constant Values: literals, enums or final vars
The for Statement
(3)
for(initialization; booleanExpression; updateStatement) { // Body }
Curly braces required for block of multiple statements, optional for single statement
Infinite Loop:
for( ; ; ) {System.out.println(“Hello World”); }
foreach:
for(datatype instance : collection) { //body }
Labels and break
(3)
optionalLabel: while(booleanExpression) { // Body // Somewhere in loop break optionalLabel; }
optionalLabel: while(booleanExpression) { // Body // Somewhere in loop continue optionalLabel;
}
While the break statement transfers control to the enclosing statement, the continue statement transfers control to the boolean expression that determines if the loop should continue