# Chapter 2 - Operators and Statements Flashcards Preview

## Java OCA Exam > Chapter 2 - Operators and Statements > Flashcards

Flashcards in Chapter 2 - Operators and Statements Deck (9)
Loading 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 : =, +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>=

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.
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
4
Q

Compound Assignment Operators

A

long x = 10;

int y = 5;

y = y * x; –> does not compile

y *= x; –> compiles

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.
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.
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
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
}```
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