Java
- Use UPPER class for the class name
- Namespace: divide different functions, etc.
- Shadowing: declare the same para as the passing one -> error: misleading
- Order of declarations doesn't matter (unlike C)
Abstract Data Types (ADT) (distinguished from primitive non-object types)
- key idea: information hiding
- ADT = data + operation (interface to work with itself)
- benefits x2: (mainly for clients)
- client doesn't need to understand the implementation
- implementation change doesn't affect clients
- Fixed bytes (unlike C depending on compilers and OS)
- Don't use floating points for money! (use 2 int instead)
Java collection
Comparison with C
Garbaging
- C: manually allocate & garbage (
new
& delete
, malloc
& free
)
- Java: Use
new
and no free, creating pointers (use . Instead of -> inside the class methods though), therefore very safe & hides everything
Static keyword
static
: indicates the class method, invoked as ClassName.methodName()
- non-
static
: instance method, use this
instead or just drop this.
if you don't shuttle
Not accessible outside if not public
Method Definition Order
- for Java, methods may appear in any order (called before definition is allowed) unlike C
- same rules within the function (the variable declaration orders)
Conditionals