Site hosted by Angelfire.com: Build your free website today!
scjp section 2: flow control, assertions and exception handling
 
  • Write code using if and switch statements and identify legal argument types for these statements.

The Selection Statements

  1. if()/else
  2. switch
  3. switch (variable) { variable must be assignment compatible with an int ie. byte, short, char or int
        case constant: <code;> constant can be a constant expression that can be evalutated at compile-time
                       <break;> if no break continues with code down list till end of switch statement, or finds a break
        default: <code;> default can be any where in list of cases.
    }  

  • Write code using all forms of loops including labeled and unlabeled, use of break and continue, and state the values taken by loop counter variables during and after loop execution.

The Loop Constucts

  1. The while() Loop
  2. while (boolean_condition) {
        repeated_block_or_statement;
    }
    { and } are optional if just one statement in block, but good practice to always use

  3. The do/while() Loop
  4. do {
        repeated_block_or_statement;
    } while (boolean_condition)
    { and } are optional if just one statement in block
    will execute block at least once

  5. The for() Loop
    • for(setup_statement; condition; iteration_expression) {
          repeated_block_or_statement;
      }
    • for (;;) creates an infinite loop
    • in the setup statement can't have expressions (eg. i++), or declarations of mixed types (eg. int i, long l), but can have multiple declarations of the same type. Also, cant mix declarations of one variable with assignment of another.

  6. continue
    • jumps to next iteration of most inner loop
    • can name loops so can break/continue out of a specified loop
        eg.   outer: for (int i = 0; ...) {
          inner: for (int j = 0; ...) {
              if (condition) {
                  continue outer;
              }
          }
      }

  7. break
    • breaks out of inner, or specified, loop

  • Write code that makes proper use of exceptions and exception handling clauses (try, catch, finally) and declares methods and overriding methods that throw exceptions.
  • Recognise the effect of an exception arising at a specified point in a code fragment. Note: The exception may be a runtime exception, a checked exception, or an error (the code may include try, catch, or finally clauses in any legitimate combination).

Exceptions

  1. try/catch/finally
  2. try {
    if exception not caught, executes finally block then jumps out of method and method throws the exception
    } catch (NameOfException e) {
    } finally {
    once execution enters a try block will always get to finally block (either after try executed successfully, after a catch block or when code in try block throws an unhandled exception
    }  

  3. finally block
  4. can only get missed if:
    • exception arises in the finally code
    • death of thread, use of System.exit() or power off

  5. Catching Multiple Exceptions
  6. - only one catch code gets executed, so put most specific first

  7. Throwing Exceptions
    • how to...
      1. create an instance of a subclass of java.lang.Throwable
      2. throw it
    • best to combine eg. throw new IOException("file not found");

  8. Uncaught Checked Exceptions
  9. must be declared to be thrown by the method
      e.g. myMethod throws IOException, Exception {
    NOTE: order is important here too

  10. java.lang.Throwable

  11. ||_java.lang.Error (errors - should be rare, and usually unrecoverable)
    |__java.lang.Exception (checked exceptions)
         |_java.lang.RuntimeException (program bugs eg. ArithmeticException (divide by 0), NumberFormatException (eg. new Integer("ten")), IllegalMonitorStateException (if have a call to wait() or notify() not in synchronized code) and ArrayIndexOutOfBoundsException)

  12. Sub-classing and Overriding a Method
  13. that throws a checked exception
    the subclass may also throw the exception or a subset of the exceptions thrown by the parent - it cannot throw a superclass of the exception, or any other new type of exception

  • Write code that makes proper use of assertions, and distinguish appropriate from inappropriate uses of assertions.
  • Identify correct statements about the assertion mechanism.

Assertions

  1. assert boolean_expression; or assert boolean_expression:argument;

  2. if boolean_expression is false, then argument(which can be any object or primitive) gets passed to an AssertionError which uses it (using toString() on the object) to create it's error message

  3. Compile-time
  4. to treat the assert as a keyword: javax -source 1.4 AppName.java

  5. Run-time
  6. assertions are disabled by default so enable/turn on with: java -enableassertions AppName
    can use -ea instead of -enableassertions