Topic
V Java concepts (for Lightning Java, Fall 2015)
V Expressions
> Low-level / atomic expressions
> Operator expressions
V Statements
> General aspects of statements
> Iteration statements (loops)
> Conditional statements
> Branching statements
V Arrays and other collections
> An array is a linearly-ordered collection (set) of Objects or values
> Arrays and initialization
> Access to elements through indexing
* ArrayLists are objects that encapsulate an array and allow access to it via methods, including ones which seem to extend the size of the collection
* In fact, when an ArrayList needs to be enlarged, a new array is allocated internally, and the old values copied over
* (For this reason, ArrayLists should be used carefully: the illusion of extendability comes with a cost.)
* Other Java objects implementing various forms of aggregates are part of the Collections framework (not on the exam!)
V Methods
* Methods are like sub-routines, functions, procedures and similar features in other languages: they collect together statements which suit some purpose and are parameterized to allow for greater flexibility
* In Java, methods execute in the context of some objects local state (i.e., collection of variables), and thus are normally used to update the variables in that state (but also to communicate with other objects by calling their methods)
> Rationale for methods
> Uses of methods
> Methods and return values
> Method header syntax: a method header has a return type (possibly void), a method name, a parameter list (each one a typed variable) and an optional throws clause (see Exceptions below)
* local variables may be declared inside a method—they disappear upon return of the method
* method input and output: void return type and empty parameters possible
V Objects and classes
> Objects are encapsulated collections of variables (called a state) and sets of methods which act upon them
> Static and non-static
> Constructors
> Class hierarchy and inheritance
V Interfaces
* A good object-oriented design will often include different classes which nevertheless provide some of the same capabilities, in terms of meaningful method calls
* Java provides a useful notion of contract which allows these common capabilities to be specified by the programmer and checked by the compiler—this facilitates re-factoring of designs
* Interfaces are collections of method names and signatures: but no method bodies!
> Thus an interface allows us to specify a set of capabilities without (yet) specifying how they will be implemented
> Signatures include the name of a method, the type of the result and the number and type of the arguments
> A class can be declared as implementing an interface: class A implements I { ... }
* One interface may be declared as extending another, so as to provide some (hierarchical) structure, just like with sub-classes and super-classes
> Abstract classes are "halfway between" a class and an interface
V Exceptions
* Exceptions are a program structuring mechanism which allow us to ignore potential errors for the duration of longer blocks of code
* Exceptions allow us to concentrate on what is likely to happen, and to restrict attention to errors to specific locations in the code
* Exceptions are objects of (a sub-class of) the Exception class in Java
> Exceptions are thrown (either by the programmer or the system) when something goes wrong
> Exceptions can be caught using a try-catch block
* Methods which might throw an exception without catching it must be marked with a clause “throws Exception” in their header (even if the throw comes indirectly from a method they call)
* Some exceptions (array index out-of-bounds, division by zero and null reference access) are so common that they do not need to be explicitly marked with throws clauses: these are called unchecked exceptions
* When an event is thrown, it propagates up the chain of called methods, eliminating them until a catch block is found