|
return to computer science and java
Section 1: Declarations and Access Control
Section 2: Flow Control, Assertions, and Exception Handling
Section 3: Garbage Collection
Section 4: Language Fundamentals
Section 5: Operators and Assignments
Section 6: Overloading, Overriding, Runtime Type and Object Orientation
Section 7: Threads
Section 8: Fundamental Classes in the JAVA.LANG Package
Section 9: The Collections Framework
|
|
- Write code using if and switch statements and identify legal argument types for these statements.
The Selection Statements
if()/else
switch
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
- The
while() Loop
while (boolean_condition) {
repeated_block_or_statement;
} |
{ and } are optional if just one statement in block, but good practice to always use |
- The
do/while() Loop
do {
repeated_block_or_statement;
} while (boolean_condition) |
{ and } are optional if just one statement in block will execute block at least once |
- 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.
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;
}
}
}
|
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
try/catch/finally
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 |
} |
|
finally block can only get missed if:
- exception arises in the finally code
- death of thread, use of
System.exit() or power off
- Catching Multiple Exceptions - only one catch code gets executed, so put most specific first
- Throwing Exceptions
- how to...
- create an instance of a subclass of
java.lang.Throwable
- throw it
- best to combine eg.
throw new IOException("file not found");
- Uncaught Checked Exceptions must be declared to be thrown by the method
e.g. myMethod throws IOException, Exception {
NOTE: order is important here too
- java.lang.Throwable
||_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)
- Sub-classing and Overriding a Method 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
assert boolean_expression; or assert boolean_expression:argument;
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
- Compile-time
to treat the
assert as a keyword: javax -source 1.4 AppName.java
- Run-time
assertions are disabled by default so enable/turn on with:
java -enableassertions AppName
can use -ea instead of -enableassertions
|