Java Tutorials : Java Operators & Control Flow Statement Explained
5. Bit-wise operators - &, ^, |
- Operate on numeric and Boolean operands.
- & - AND operator, both bits must be 1 to produce 1.
- - OR operator, any one bit can be 1 to produce 1.
- ^ - XOR operator, any one bit can be 1, but not both, to produce 1.
- In case of Booleans true is 1, false is 0.
- Can't cast any other type to Boolean.
6. Short-circuit logical operators - && , ||
- Operate only on Boolean types.
- RHS might not be evaluated (hence the name short-circuit), if the result can be determined only by looking at LHS.
- False && X is always false.
- True || X is always true.
- RHS is evaluated only if the result is not certain from the LHS.
- That's why there's no logical XOR operator. Both bits need to be known to calculate the result.
- Short-circuiting doesn't change the result of the operation. But side effects might be changed. (i.e. some statements in RHS might not be executed, if short-circuit happens. Be careful)
7. Ternary operator
- Format a = x ? b : c ;
- x should be a Boolean expression.
- Based on x, either b or c is evaluated. Both are never evaluated.
- b will be assigned to a if x is true, else c is assigned to a.
- b and c should be assignment compatible to a.
- b and c are made identical during the operation according to promotions.
8. Assignment operators
- Simple assignment =.
- op= calculate and assign operators(extended assignment operators)
- *=, /=, %=, +=, -=
- x += y means x = x + y. But x is evaluated only once. Be aware.
- Assignment has value, value of LHS after assignment. So a = b = c = 0 is legal. c = 0 is executed first, and the value of the assignment (0) assigned to b, then the value of that assignment (again 0) is assigned to a.
- Extended assignment operators do an implicit cast. (Useful when applied to byte, short or char)
byte b = 10;
b = b + 10; // Won't compile, explicit cast required since the expression evaluates to an int
b += 10; // OK, += does an implicit cast from int to byte
d. Control flow statement
1. Decision-making statements:
- If-then statements are used for decision-making based on a condition. If statement tells your program to execute a certain section of code only if a particular condition evaluates to true. If this condition evaluates to false the control jumps to the end of the if-then statement and statements after that are executed.
- The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false.
2. Looping statements: There are three looping statements-for, while, do.
- In while and for, the test occurs at the top, so if the test fails at the first time, body of the loop might not be executed at all.
- In do, test occurs at the bottom, so the body is executed at least once.
- In for, we can declare multiple variables in the first part of the loop separated by commas, also we can have multiple statements in the third part separated by commas
- Switch can used for making selection statements. Switch statement checks for a condition and based on result selects one out of many cases provided to execute statements. Switch statement can use primitive data types, String class and Enums.
- The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, and then executes all statements that follow the matching case label.
4. Branching statements : Break statement can be used with any kind of loop or a switch statement or just a labeled block. Continue statement can be used with only a loop (any kind of loop). Loops can have labels. We can use break and continue statements to branch out of multiple levels of nested loops using labels.