-
Chapter 1: Declaration, Access Control
-
Identifier/JavaBeans
- Legal Identifiers: start with letter, $, _
- Keywords
-
Sun's Java Code Conventions
- Class Name
- Method
- Variable
- Constant
-
JavaBeans Standards
- get/set method, public
- boolean: is/get
- event/listener, addActionListener()
-
Class Declaration
-
Source File
- one public class per source file
- comments are anywhere
- public class name matches the file name
- package should before import
-
Modifier
- Access: public, protected, private, default(package level)
-
Non-Access: strictfp, final, abstract
- strictfp: conform to IEEE 754 floating point
- final: cannot be subclassed, java.lang.String
- abstract: can never be instantiated, is to be extended
-
Interface Declaration
- Methods are implictly public and abstract, and cannot be static, final, strictfp or native
- Variables defined must be public, static and final
- Extends from 1 or more interfaces, not implements
- Cannot extend anything but interface
-
Class Member
-
Access modifier
- public: all other classes, regardless of the package they belong to, can access
- private: only the class which private member is declared
-
protected/default
- subclass can see protected member only through inheritance
- Local variables
-
Nonaccess member modifier
- final method
- final arguments
- abstract method
- synchronized method
- native method
- strictfp method
- variable argument list method
- Constractor
-
variable
- primitive
- range
- reference
- local variable: always on the stack, rather than a heap
-
enum
- cannot declare enum in a method
- neither String nor int
- special kind of class
- Wrong subject: 6, 8 and 9
-
Chapter 2: Object Orientation
- Topic
-
Is-A
- Cannot extend more than one class
- Has-A
-
Overriding
- Argument list should exactly match
- Return type should the same as or a subtype of
- Access level can't be more restrictive
- Can throw runtime exception
- Cannot throw checked exception
- Can throw narrower/fewer exceptions
- Cannot override final/static method
-
Overloading
- must change the argument list
- can change the return type
- can change the access modifier
- can declare new/broader checked exception
-
Reference Casting
- downcasting
- upcasting
-
Implement interface
- A class can implement more than one interface
- interface self extend another interface
-
Return Type
- Covariant returns in overriding return type which can be a sub class
- Return value
-
Constructor and Instantiation
- every class MUST have a constructor
-
Constructor Chaining
-
Horse h = new Horse()
- Animal()
- Object()
-
Rules
- any access modifier
- match the class name
- no return type
- same name method doesn't make it constructor
- default constructor will be automatically generated if no constructor was typed
- default constructor ALWAYS a no-arg constructor
- once one constructor is typed, no default will be generated
- every constructor first either call this() or super()
- cannot call directly the constructor
- abstract has while interface hasn't
-
default constructor
- same access modifier as the class
-
Static variables & Method
- static method cannot access a non-static variable
- static method cannot access a non-static method
- static method cannot be overridden
- Wrong subjects: 3, 5, 11
-
Chapter 7: Generic & Collections
-
Class methods
- toString()
-
equals()
- reflexive
- symmetric
- transitive
- consitent
-
hashCode()
- a.equals(b) -> a.hashCode()==b.hashCode()
- a.hashCode()!=b.hashCode() -> a.equals(b) == false
- transient variable may cause fail
-
Collections
-
<<interface>> java.util.Collection
-
<<interface>>List
- ArrayList
- Vector
- LinkedList
- get(int index), indexOf(int index), add(int index, Object obj), etc.
- Iterator: hasNext(), next()
-
<<interface>> Set
- SortedSet
- HashSet
- LinkedHashSet
- TreeSet
-
<<interface>> Queue
- PriorityQueue
- offer(), poll(), peak()
-
<<interface>>Map
- SortedMap
- HashMap
- Hashtable
- TreeMap
- LinkedHashMap
-
Ordered & Sorted
- alphabetically
- ascending
- implements Comparable
- Comparator
-
utility
-
java.util.Collections
- sort()
- Comparable: compareTo()
- Comparator:compare()
- binarySearch()
- void reverse()
- reverseOrder()
- return a Comparator that sorts in reverse
-
java.util.Arrays
- binarySearch()
- sort()
- asList(T[])
-
Generic
- MUST be the exactly type in generic when initialize a generic collection
-
Methods
- method(List<? extends Base> parameter)
- method(List<? super Child> parameter)
- method(List<?> list)
- ? means many possibilities
- only for reference declarations
-
Class
- class SomeGeneric<T>
- class SomeGeneric<T extends Base>
-
Generic Methods
- public <T> void method(T t)
- public <T extends Number> void method(T t)
- Wrong Exercise: 1, 4, 5, 9, 10, 12, 14 and 16
-
Chapter 9: Threads
-
define
-
Extends java.lang.Thread
-
constructor
- Thread()
- Thread(Runnable target)
- Thread(Runnable target, String name)
- Thread(String name)
- Implements Runnable
-
Starting Thread
- t.start()
- A thread done when its target run() completes
- Once it has been started, it can never be started again
- Scheduler
-
States
-
New
- before start() invoked, not alive
- Runnable
- Running
- Waiting / blocked / sleeping
- Dead
-
Priorities
- 1~10
- NEVER rely on it to guarantee the correctness
-
yield()
- allow other threads of the same priority to get their turn
-
Synchronization
- methods and blocks
- sleep doesn't release the lock
- each object has just one lock
- static method can be synchronized
-
When cannot get lock
- static synchronized always block each other
- method and lock status
- methods that access changeable fields need to be synchronized
-
Thread-Safe Class
- Collections.synchronizedList()
- Deadlock