Certification Summary


This chapter covered a lot of ground, all of which involves ways of controlling your program flow, based on a conditional test. First you learned about if and switch statements. The if statement evaluates one or more expressions to a boolean result. If the result is true, the program will execute the code in the block that is encompassed by the if. If an else statement is used and the if expression evaluates to false, then the code following the else will be performed. If no else block is defined, then none of the code associated with the if statement will execute.

You also learned that the switch statement can be used to replace multiple if-else statements. The switch statement can evaluate integer primitive types that can be implicitly cast to an int (those types are byte, short, int, and char), or it can evaluate enums.

At runtime, the JVM will try to find a match between the expression in the switch statement and a constant in a corresponding case statement. If a match is found, execution will begin at the matching case, and continue on from there, executing code in all the remaining case statements until a break statement is found or the end of the switch statement occurs. If there is no match, then the default case will execute, if there is one.

You've learned about the three looping constructs available in the Java language. These constructs are the for loop (including the basic for and the enhanced for which is new to Java 5), the while loop, and the do loop. In general, the for loop is used when you know how many times you need to go through the loop. The while loop is used when you do not know how many times you want to go through, whereas the do loop is used when you need to go through at least once. In the for loop and the while loop, the expression will have to evaluate to true to get inside the block and will check after every iteration of the loop. The do loop does not check the condition until after it has gone through the loop once. The major benefit of the for loop is the ability to initialize one or more variables and increment or decrement those variables in the for loop definition.

The break and continue statements can be used in either a labeled or unlabeled fashion. When unlabeled, the break statement will force the program to stop processing the innermost looping construct and start with the line of code following the loop. Using an unlabeled continue command will cause the program to stop execution of the current iteration of the innermost loop and proceed with the next iteration. When a break or a continue statement is used in a labeled manner, it will perform in the same way, with one exception: the statement will not apply to the innermost loop; instead, it will apply to the loop with the label. The break statement is used most often in conjunction with the switch statement. When there is a match between the switch expression and the case constant, the code following the case constant will be performed. To stop execution, a break is needed.

You've seen how Java provides an elegant mechanism in exception handling. Exception handling allows you to isolate your error-correction code into separate blocks so that the main code doesn't become cluttered by error-checking code. Another elegant feature allows you to handle similar errors with a single error-handling block, without code duplication. Also, the error handling can be deferred to methods further back on the call stack.

You learned that Java's try keyword is used to specify a guarded region—a block of code in which problems might be detected. An exception handler is the code that is executed when an exception occurs. The handler is defined by using Java's catch keyword. All catch clauses must immediately follow the related try block. Java also provides the finally keyword. This is used to define a block of code that is always executed, either immediately after a catch clause completes or immediately after the associated try block in the case that no exception was thrown (or there was a try but no catch). Use finally blocks to release system resources and to perform any cleanup required by the code in the try block. A finally block is not required, but if there is one it must immediately follow the last catch. (If there is no catch block, the finally block must immediately follow the try block.) It's guaranteed to be called except when the try or catch issues a System.exit().

An exception object is an instance of class Exception or one of its subclasses. The catch clause takes, as a parameter, an instance of an object of a type derived from the Exception class. Java requires that each method either catches any checked exception it can throw or else declares that it throws the exception. The exception declaration is part of the method's public interface. To declare that an exception may be thrown, the throws keyword is used in a method definition, along with a list of all'checked exceptions that might be thrown.

Runtime exceptions are of type RuntimeException (or one of its subclasses). These exceptions are a special case because they do not need to be handled or declared, and thus are known as "unchecked" exceptions. Errors are of type java.lang.Error or its subclasses, and like runtime exceptions, they do not need to be handled or declared. Checked exceptions include any exception types that are not of type RuntimeException or Error. If your code fails to either handle a checked exception or declare that it is thrown, your code won't compile. But with unchecked exceptions or objects of type Error, it doesn't matter to the compiler whether you declare them or handle them, do nothing about them, or do some combination of declaring and handling. In other words, you're free to declare them and handle them, but the compiler won't care one way or the other. It's not good practice to handle an Error, though, because you can rarely recover from one.

Exceptions can be generated by the JVM, or by a programmer.

Assertions, added to the language in version 1.4, are a useful debugging tool. You learned how you can use them for testing, by enabling them, but keep them disabled when the application is deployed. If you have older Java code that uses the word assert as an identifier, then you won't be able to use assertions, and you must recompile your older code using the -source 1.3 flag. Remember that as of Java 5, assertions are compiled as a keyword by default, but must be enabled explicitly at runtime.

You learned how assert statements always include a boolean expression, and if the expression is true the code continues on, but if the expression is false, an AssertionError is thrown. If you use the two-expression assert statement, then the second expression is evaluated, converted to a String representation and inserted into the stack trace to give you a little more debugging info. Finally, you saw why assertions should not be used to enforce arguments to public methods, and why assert expressions must not contain side effects!




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net