Prepare for OCAJP, (2)
Java Operators and Statements.
Unary Operators
x++
vs. ++x
++x
will returnx+1
x++
will return x, than incrementint counter = 0; System.out.println(counter); // Outputs 0 System.out.println(++counter); // Outputs 1 System.out.println(counter); // Outputs 1 System.out.println(counter--); // Outputs 1 System.out.println(counter); // Outputs 0
Binary Operators
- Numeric Promotion Rules
- If two values have different data types, Java will promote one to the larger of the two data types
- If one value is intergral and another is floating-point, intergral promoted to floating-point
- byte, short,char will first pomoted to int. Even if neither of the operands is int
- In the end, resulting value will have same data type as its promoted operands
short x = 10; short y = 3; short z = x * y; // DOES NOT COMPILE, int*int cannot assign to short short z = (short)(x * y);
- Overflow vs. Underflow
Overflow is when a number is so large that it will no longer fit within the data type, so the system “wraps around” to the next lowest value and counts up from thereSystem.out.print(2147483647+1); // -2147483648
Since 2147483647 is the maximum int value, adding any strictly positive value to it will cause it to wrap to the next negative number.
- Compound operators
Compound operators can also save us from having to explicitly cast a value.long x = 10; int y = 5; y = y * x;// DOES NOT COMPILE, becasue data types y *= x;
- Logical Operators
- AND is only true if both operands are true.
- Inclusive OR is only false if both operands are false.
- Exclusive OR is only true if the operands are different.
Ternary Operators
As of Java 7, only one of the right-hand expressions of the ternary operator will be evaluated at runtime.
SWITCH
A switch statement has a target variable that is not evaluated until runtime
Note that boolean and long, and their associated wrapper classes, are not supported by switch statements.
The exam creators are fond of switch examples that are missing break statements!
private int getSortOrder(String firstName, final String lastName) {
String middleName = "Patricia";
final String suffix = "JR";
int id = 0;
switch(firstName) {
case "Test":
return 52;
case middleName:// DOES NOT COMPILE, String is not final
id = 5;
break;
case suffix:
id = 0;
break;
/*despite lastName being final,
*it is not constant as it is passed to the function
*/
case lastName: // DOES NOT COMPILE
id = 8;
break;
case 5: // DOES NOT COMPILE
id = 7;
break;
case 'J': // DOES NOT COMPILE
id = 10;
break;
case java.time.DayOfWeek.SUNDAY:// DOES NOT COMPILE
id=15;
break;
}
return id;
}
FOR LOOP
- Redeclaring Varialbes in Initialization Block
int x = 0; for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) { // DOES NOT COMPILE System.out.print(x + " "); }
Note: The difference is that x is repeated in the initialization block after already being declared before the loop, resulting in the compiler stopping because of a duplicate variable declaration
- Incompatible Data Types in Initialization Block
for(long y = 0, int x = 4; x < 5 && y<10; x++, y++) { // DOES NOT COMPILE System.out.print(x + " "); }
The variables in the initialization block must all be of the same type