Chapter 2 - Operators and Statements Flashcards

1
Q

Operator Precendence

(12)

A

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 : =, +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>=

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

Numeric Promotion

(4)

A
  1. If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
  2. 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.
  3. 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.
  4. 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

Unary Operators

(5)

A
    • 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
  1. ++ Increments a value by 1
  2. – Decrements a value by 1
  3. ! Inverts a Boolean’s logical value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Compound Assignment Operators

A

long x = 10;

int y = 5;

y = y * x; –> does not compile

y *= x; –> compiles

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

Equality Operators

(3)

A
  1. 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.
  2. Comparing two boolean values.
  3. Comparing two objects, including null and String values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ternary Operator

(2)

A
  1. booleanExpression ? expression1 : expression2
  2. As of Java 7, only one of the right-hand expressions of the ternary operator will be evalu- ated at runtime.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Switch Statement

(6)

A

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

The for Statement

(3)

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

Labels and break

(3)

A
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

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