Difference: final, finally, finalize
final is used to control whether a variable, method or class is "changeable".
- variable:
- primitive: the value can not change.
- reference: the reference variable cannot point to any other object on the heap.
- method: cannot be overriden
- class: cannot be subclass
- variable:
finally is used in try/catch block to ensure a segment code is always executed.
- always executed even if an exception is thrown or try to return/continue/break statement in the try block(except JVM exits from the try block)
- often used to write the clean-up code. will be executed after the try and catch blocks, before return statement
finalize() is called by Garbage Collector once it determines there is no more reference exists.
HashMap vs TreeMap vs LinkedHashMap
- HashMap: O(1) lookup & insertion. Implemented by an array of linked lists.
- TreeMap: O(log n) lookup & insertion. Implemented by a red-black tree.
- LinkedHashMap: O(1) lookup & insertion. Keys are ordered by their insertion order. Implemented by doubly-linked buckets.