Chapter 2 - Java operators Flashcards
What are the three flavors of operators available in Java?
three flavors of operators are available in Java: unary, binary and ternary.
What is the order Java operators follow?
Java operators follow order of operation. If 2 operators have the same level of precedence, then Java guarantees left-to-right evaluation.
Arithmetic operators may be apply to? And except to which type?
All of the arithmetic operators may be applied to any Java primitives. EXCEPT boolean and String.
Which are the only arithmetic operators that can be applied to Strings?
Only the addition operators + and += may be applied to String values, which result in String concatenation.
In numeric promotion, if two values have different data types, what Java does?
If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.
In numeric promotion, if one of the values is integral and the other is floating point, what will Java do?
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.
In numeric promotion, to which type are smaller data types (byte, short and char) promoted to?
Smaller data types, namely byte, short and char, are first promoted to int any time they’re used with Java binary arithmetic operator, even if neither of the operands is int. **important: unary operators are excluded from this rule. I.e: applying ++ to a short value results in a short value.
What is the resulting value after a promotion has been done and all the operants have the same data type?
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.
When references are equal?
Two references are equal if and only if they point to the same object, or both point to null.
Which data types are supported by Switch statements?
Data types supported by switch statements:
byte and Byte short and Short char and Character int and Integer String enum values
Are boolean and long supported by Switch statements?
No. Boolean and long and their associate wrapper classes are not supported by switch statements.
What is the variable’s coverage in the initialization of a FOR loop?
Variables declared in the initialization block of a for loop have limited scope and are only accessible within the for loop.
Does below code compile? If so, which will be the output?
for ( ; ;) {
}
The above will compile and run without issue. The output is an infinite loop.
Components of the for loop are each optional BUT the semicolons ; are required as for ( ) will not compile.
In a FOR loop, can a variable be redeclare in the initialization block (i.e. x below)
int x = 0;
for(long y = 0, x = 4; x < 5 && y < 10; x++, y++){
}
No, the above will not compile because x is initialized 2 times; outside of the for and inside the for statement.
Can different data types be used in the initialization block of a FOR loop?
for(long y = 0, int x = 4; x < 5 && y < 10; x++, y++){
}
No, the variables in the initialization block must all be of the same type.