Great book written by Kathy Sierra and Bert Bates.
I wanted to share few important quotations found from the fifth chapter.
1) The general form of the switch statement is:
switch (expression) {
case constant1: code block
case constant2: code block
default: code block
}
2) It's also illegal to have more than one case label using the same value. For example, the following block of code won't compile because it uses two cases with the same value of 80:
int temp = 90;
switch(temp) {
case 80 : System.out.println("80");
case 80 : System.out.println("80"); // won't compile!
case 90 : System.out.println("90");
default : System.out.println("default");
}
3) It is legal to leverage the power of boxing in a switch expression. For instance, the following is legal:
switch(new Integer(4)) {
case 4: System.out.println("boxing is OK");
}
4) We can have only one test expression in a for loop.
Ex: The below code is illegal
for (int x = 0; (x > 5), (y < 2); x++) { } // too many expressions
The compiler will let you know the problem:
TestLong.java:20: ';' expected
for (int x = 0; (x > 5), (y < 2); x++) { }
5) continue statements must be inside a loop; otherwise, you'll get a compiler error. break statements must be used inside either a loop or switch statement. (Note: this does not apply to labeled break statements.).
6) The continue statement causes only the current iteration of the innermost loop to cease and the next iteration of the same loop to start if the condition of the loop is met. When using a continue statement with a for loop, you need to consider the effects that continue has on the loop iteration. Examine the following code:
for (int i = 0; i < 10; i++) {
System.out.println("Inside loop");
continue;
}
The question is, is this an endless loop? The answer is no. When the continue statement is hit, the iteration expression still runs! It runs just as though the current iteration ended "in the natural way."
7) A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. Even if there is a return statement in the try block, the finally block executes right after the return statement is encountered, and before the return executes!
1 comment:
Nice post. This blog link should be on my favorites.
Post a Comment