what is object?
an object has state,behaviors and identity
cleanup: finalization and garbage collection
- Your objects might not get garbage collected.
- Garbage collection is not destruction.
- Garbage collection is only about memory.
how a garbage collector works
- reference counting
- adaptive garbage-collection scheme
- stop-and-copy
- mark and sweep
- initialization order: static(static block) -> field -> constructor
- array & enum
access control: access specifiers
class defines what an object is, interface defines what an object can do ,access specifiers make boundaries
two benefits
1. keep users' hands off portions that they shouldn't touch.
2. allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer.
- default : package access
- protected: inheritance access (package + descendent)
- public : interface access (everyone)
- private : self;
- private & protected cannot used on class(excepted inner class)
reusing classes
- how to implement lazy load?
- composition vs inheritance
- final
- claim it is final version ,cannnot be modified
- can be used upon data(primitive/reference) , methods, classes; take attention the different effect on each target
- static final primitives are compile-constants ,named with capitals and separated with underscores.
- how about constant memory allocation?
polymorphism
separate the things that change from the things that stay the same.
General guide: use inheritance to express differences in behavior, and fields to express variations in state.
- late binding, how java implement, mechanism?
- late binding save you lots of overloads
- cannot override parent's private and static methods
- not work at direct-accessed field
- RTTI: runtime type information
- auto upcast, force downcast(ways?)
interfaces
decoupling!!!
decoupling interface from implementation allows an interface to be applied to muliple different implementations, and thus your code is more reusable.
abstract class defined what it is like, interface say what it can do
any abstraction should be motivated by a real need. when necessary
- abstract class
- interface
- method works with interface instead of class, loosen the limit of forceing params be subclasses
- you should always prefer interfaces to abstract classes
releated design patterns: strategy, adatper, factory
inner classes
- closure , callbacks
- .this .new
- prevent any type-codeing dependency and completely hide details about implementation
- anonymous class
- nested class (static inner class)
holding your objects
task:
- get familiar with common containers
- java.util.collection source code
- java.util.Collections, java.util.Arrays
- 数据结构的实现
- interface:collection,list, iterator
- org.springframework.util.collections
exception
task:
- take use of exception mechanism to complete a general api call process dealing with different
- common types of exception
- try/catch , if no exception occurs there is little cost
- checked exception vs. unchecked exception
- Error, Exception, RuntimeException
- exception specification interface for a particular method may narrow during inheritance and overriding, but it may not widen
Strings
string is immutable!!!
questions:
- why string is immutable? and how
- formatter classes
- code structure
RTTI(runtime type information)
- class loader, mechanism
- 基于反射实现java版本重试
- java.lang.reflect Proxy.newProxyInstance动态代理
- Null Objects
Generics
by knowing what you can't do, you can make better use of what you can do
the generic types are present only during static type checking, after which every generic type in the program is erased by replacing it with none-generic upper bound
- 实现.net的Tuple功能
- generic class, inteface, method
- type inference: only work when assignment
erasure - migration compatability
- problem with erasure:
- cannot use as specific type,
- type information is lost
- ensure type consistent during compile time
- ? extends vs ? super
- wildcards , bound and unbound
- design pattern: decorator/adatper/strategy
reference:
arrays
- initialization
- Arrays.xx
- java.lang.Comparable
containers in depth
design pattern: flyweight
Map : HashMap/LinkedMap/TreeMap(red-black)/WeakHashMap/ConcurrentHashMap/IdentityHashMap
Map.Entry, AbstractMap, AbstractSet
Arrays.asList : return a fixed-size list
Set: HashSet/TreeSet/LinkedHashSet
hashcode used for hashXX
Map.Entry
-
source and data structure of HashMap
-
how to correctly override hashCode()?
- identical 恒等
- fast and meaningful 考虑查找效率
- in an even distribution of values 分布平均
rehashing
performance test
I/O
- FileChannel,xxStream.getChannel, ByteBuffer -> asXXBuffer(view buffer), Charset
- clear/flip/rewind
- nio: MappedByteBuffer
- compression
- decorator pattern
enums
- static import
- the mystery of values(): static method added by the compiler
- Class.getEnumConstants()
- EnumSet are built on top of longs which is 64bit; it's ok when it contains more than 64 enums , think why
- < 64 RegularEnumSet; >= 64 JumboEnumSet
- EnumMap
- command design pattern using enums is so fantastic
- chain of responsibility pattern
- state machine
- multiple dispatching -> 2D Array
annotations
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constrains{
boolean primaryKey() default false;
boolean allowNull() default true;
boolean unique() default false;
}
@Target(ElementType.FIELD)
@Retention(RetetionPolicy.RUNTIME)
public @interface SQLString{
int value() default 0;
String name() default "";
Constraints constraints() default @Constraints;
}
concurrency
- it seems counterintuitive that multithreaded program needs context switching cost
- concurrency imposes costs, including complexity costs, but these are usually outweighted by improvements in program design, resource balancing, and use conveniece
- interface: Runnable vs Callable
- enum TimeUnit
- ExecutorService exec = Executors.newCachedThreadPools / newFixedThreadPool
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
- Executors
- Future FutureTask
- Sleeping: TimeUnit.MILLISECONDS.sleep(X);
- daemon thread, gc is a daemon thread in jvm?
- how Executors propagate exception
- getUncaughtExceptionHandler()
- volatile
- atomicity applies simple operation on primitive types except for long and double which are two seperate 32-bit operations
- volatile ensures visibility across application, as soon as a write occurs for that field, all reads will see the change
- java's increment a++ is not atomitic ,it involve both a read and a write
- Thread local storage, ThreadLocal
- BlockingQueue、CountDownLatch、CyclicBarrier、Semaphore
performance tune
- synchronized -> lock / atmoic
- lock-free container: CopyOnWriteArrayList ,ConcurrentHashMap
- ReadWriteLock
- Active Object, actors
task:
- wirte a general task util simplifying thread operations
- how jvm lock ja
- Lock, ReentranceLock, ReadWriteLock,Condition
- long vs double
THEN
- JDK: collection, exception,reflect.proxy, concurrency, nio...etc
- 刷题: codewars 基础部分(8kyu) 快速熟悉起来
- 《Effective Java》
- 《数据结构与算法 java语言描述》 内功 不解释