Professional Documents
Culture Documents
OCP
enums
o enum Name { VAR1,VAR2,VAR3 }
o outside class public or default modifier
o constructors -> is instantiated when instantiating 1 enum,
instance variables, methods, constant specific class body ->
overriding method of specific enum
o MyEnum.values() array of MyEnums values
interface implementation
o must have implementation of each method of interface
o no checked exceptions other than declared by interface
o maintain signature & return type of interface method
o if abstract class implementing interface no method
implementation needed!
o class can implement more than 1 interface
o interface can extend (more than 1!) other interface(s)
legal return types
o overloading = name reuse you cannot only change return
type! must change argument list
o overriding return type must be subtype of declared return
type (in 1.5)
o if primitive return type any value that can implicitly be
converted (thus for example char to int), or explicitly cast
declared
o returning object can be of subtype
constructors & instantiation
o Every class INCLUDING abstract, MUST HAVE constructor
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o initialization blocks { x = 8; }
static class is first loaded
instance instance is created after call to super()!!!
order matters!
static variables & methods
o they belong to class, not instance
o main is static can only get static variables
o nonstatic cannot access static & static cannot access nonstatic
o static variable/method Classname.method() or
Classname.variable, but you can also use an object reference
(thus ref.variable or method())
o in constructors you can declare static & nonstatic variables!
From long -> double = ok; from double -> int without
cast not!
Example: byte b = (byte) 130L; -126 no error!
float assigment MUST have cast (float) OR f behind the
value!!!
If literal value (thus numeric value) is to high to assign
compiler error
assignment operators
o size matters @ primitives
o reference variable is no object
o type matters at value
compound assignment opterators: +=, -=, *=, /=
relational operators: <, <=, >, >=, ==, !=
equality: numbers, characters, boolean primitives, object reference
.equals for String case sensitive!
instanceOf check if object reference variable is of particular
type.
o A null reference can also be tested always false of course.
o Compiler error @ two different class hierarchies!!! (if no
relation)
+, -, *, /
% modulus remainder of a division
When + sign with numbesr & String from left to right if first
numbers then numbers add + concate String. If first String then
all Strings concatenated.
increment/decrement operators: ++, -- if prefix, done before
expression, if postfix after this
o final variables cannot be changed! compiler error
conditional operator: x = (bool expression) ? value to assign if true :
value to assign if false;
logical operators: &, |, ^, !, &&, ||
o short-circuit (does not waste/perform pointless
evaluations): &&, ||
o non-circuit: &, |
o ^ exclusive-OR (XOR) ONE expression needs to be true (not
both)!
o ! boolean-invert opposite of booleans current value
(bitwise operators: &, |, ^)
String = Immutable Object!
o Each character 16-bit Unicode character
o Each declaration/editing new object creation!
o When using String method (e.g. concat, toUpperCase, replace),
but not referenced not changed
if(boolean expression){ }
o else { } optional
o else if(boolean expression) { }
o zonder {} alleen eerstvolgende regel geldt!
switch(expression: char, byte, short, int, enum, String){
o case outcome: { }
o use break!
o default: { } does not have to come at the end!
o outcome value MUST BE compile time constant!!!
o outcome value must meet expression type requirements (thus
byte type cannot take 128)
o no duplicate outcome value
o String: case-sensitive equality
while loop
o while(bool expression){ }
o do { } while(bool expression);
basic for
o for(declaration & initialization; boolean expression; iteration
expression){ }
o you can declare more of same type (!) by using commas (,)!
o break, return, System.exit() & exception cause loop
termination
o for( ; ; ;) legal!!! Some can be blank & for example bool exp
filled
advanced for
o for (type varName : list/array) { }
o break stop loop
o continue stop current iteration
o labeled loops:
exampleName: for() {}
used to define which loop to break / continue
break exampleName;
try{ } catch(Exception){ }
finally{ } always executed at some point after the try block
try{ } finally { } legal!
Exception propagation: the exception if not caught thrown to
bottom of call stack if @ main() halt JVM
Exception instance of class (subclass) of java.lang.Exception
Exception derived from Throwable ( Object). Error is also
derived from throwable (for example memory problems) can all
been thrown
printStackTrace()