|
Mon, 1 Feb
|
|
- Arrays:
- all types of arrays are Objects
- declaration eg. int a[];, Piece board[][];
- allocation eg. a=new int[5];, board=new Piece[15][]; for (int i=0; i<15; i++) board[i]=new Piece[15];
- arrays have length field (read only)
- 2D arrays do not have to be square
- Control flow:
- same as in C/C++: if-then, if-then-else, for, while, do-while,
switch-case, methods
- exceptions
- Exceptions:
- idea: try a piece of code, an error could be thrown, catch error
and handle
- try {some code} catch (Exception e) {handle error}
- somewhere inside try and error may occur and an
exception thrown
- good because your "normal case" code is not cluttered with
propagation of error cases
- control flow goes from the throw point directly
to the first matching catch on the caller stack
- if no error, then catch is skipped
- finally is ALWAYS executed no matter if error occurs or
not, but always after error-handling
- if no one catches thrown exception it gets propagated to VM,
aborting program
- subclasses of Exception (actually those implementing
Throwable) can be thrown.
- type of exception indicates error type; object can
contain information used for error handling
- object thrown is "caught" by catch clause
- if exception not caught, it can be passed on to caller by
declaring it in throws clause of method
- subclasses of RunTimeException are implicit and do not need
to be declared in throws clause
- other exceptions must either be caught and handled or
declared in throws clauses
- Inner classes:
- as of JDK 1.1 classes can be defined within classes
- used when implementing adapters, creating helper/interface objects or GUI hooks
- refer to the Java Language Specification
inner classes addendum
- useful when class is only needed temporarily or only within
scope of current class
- semantics can get a little tricky, so use only for simple
things (read reference for more detail)
- note that the virtual machine was not changed even though
the language was!
|
|
|