Monday, 11 December 2006

SCJP Chapter 5

This chapter covers flow control; conditional if and switch statements, looping do-while, while and for statements, even exceptions and assertions.

What's there to know about the if statement? It encompasses the if, if-else and if-elseif constructs. Things to beware of: unbraced statements (only one is in the if construct), lost else clause (belongs to the innermost if statement), misleading indentation and illegal expressions (must resolve to a boolean).

switch statements can simulate multiple if statements. A switch's expression takes an enum or variables that can be implicitly promoted to an int (char, byte, short, int) and it can use boxing. The value for a case must be a compile time constant. Cases with the same value will not compile. Neither will a case with a value larger than the type variable in the switch. Without break statements, the code will run top down starting from the execution entry point (the first case constant matched). This dropping down is called "fall-through". default is executed when there is no match and its location does not matter; end, middle or top.

do-while and while loops are the same except for these differences: do-while must run at least once (while may not run at all) and construction is different (note do-while ends in a semicolon).

for loops has a second structure as of Java 5. The "enhanced for loop", a.k.a for-each and for-in, makes it easier to iterate through arrays and collections. The for-each loop has two components (declaration and expression) versus the three components of a for loop (initialization, condition and iteration).

break and continue statements are used to halt a loop, either the entire loop (break) or the current iteration (continue). Labels are added when nested loops are involved, they enable the break/continue statement to apply to the labelled loop, not necessarily the loop the statement is in.

Exceptions are handled by coding in try and catch blocks. A finally block can be added and will always run after the try block, regardless whether an exception is thrown or not. A try block cannot be used on its own, either a catch or a finally block must be present. An exception must find a match with a catch block or its supertype. A method must declare an exception if it throws (directly or indirectly) the exception otherwise it must handle the thrown exception. This is the "handle or declare" rule. Runtime exceptions are exempt from this rule. Need to know which exceptions are thrown by the Java Virtual Machine (JVM exceptions) and which are thrown explicitly by application programmers (Programmatic exceptions).

Assertions test assumptions during development. Their default mode is off and must be specifically turned on. If an assertion is true, the code runs smoothly. If an assertion is false, an AssertionError is thrown, shutting down the application. Assertions have two versions, both versions have an expression that results in a boolean value, the difference is a second expression to be added to the stack for debugging purposes. Inappropriate uses of assertions are validating public method arguments, validating command-line arguments and assert expressions that cause side effects. Appropriate uses of assertions are validating private method arguments and, even in public methods, checking for cases that are not supposed to happen.

No comments: