读:java编程思想 英文版

what is object?

an object has state,behaviors and identity

cleanup: finalization and garbage collection

  1. Your objects might not get garbage collected.
  2. Garbage collection is not destruction.
  3. Garbage collection is only about memory.

how a garbage collector works

  1. reference counting
  2. adaptive garbage-collection scheme
  3. stop-and-copy
  4. mark and sweep
  5. initialization order: static(static block) -> field -> constructor
  6. 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.
  1. default : package access
  2. protected: inheritance access (package + descendent)
  3. public : interface access (everyone)
  4. private : self;
  5. private & protected cannot used on class(excepted inner class)

reusing classes

  1. how to implement lazy load?
  2. composition vs inheritance
  3. 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.
  1. late binding, how java implement, mechanism?
  2. late binding save you lots of overloads
  3. cannot override parent's private and static methods
  4. not work at direct-accessed field
  5. RTTI: runtime type information
  6. 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
  1. abstract class
  2. interface
  3. method works with interface instead of class, loosen the limit of forceing params be subclasses
  4. you should always prefer interfaces to abstract classes
    releated design patterns: strategy, adatper, factory

inner classes

  1. closure , callbacks
  2. .this .new
  3. prevent any type-codeing dependency and completely hide details about implementation
  4. anonymous class
  5. 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
  1. Error, Exception, RuntimeException
  2. exception specification interface for a particular method may narrow during inheritance and overriding, but it may not widen

Strings

string is immutable!!!

questions:

  1. why string is immutable? and how
  2. formatter classes
  3. code structure

RTTI(runtime type information)

  1. class loader, mechanism
  2. 基于反射实现java版本重试
  3. java.lang.reflect Proxy.newProxyInstance动态代理
  4. 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
  1. 实现.net的Tuple功能
  2. generic class, inteface, method
  3. type inference: only work when assignment
    erasure
  4. migration compatability
  5. problem with erasure:
    • cannot use as specific type,
    • type information is lost
    • ensure type consistent during compile time
  6. ? extends vs ? super
  7. wildcards , bound and unbound
  8. design pattern: decorator/adatper/strategy

reference:

  1. Java 泛型总结(一):基本用法与类型擦除
  2. Java泛型中extends和super的理解

arrays

  1. initialization
  2. Arrays.xx
  3. java.lang.Comparable

containers in depth

  1. design pattern: flyweight

  2. Map : HashMap/LinkedMap/TreeMap(red-black)/WeakHashMap/ConcurrentHashMap/IdentityHashMap

  3. Map.Entry, AbstractMap, AbstractSet

  4. Arrays.asList : return a fixed-size list

  5. Set: HashSet/TreeSet/LinkedHashSet

  6. hashcode used for hashXX

  7. Map.Entry

  8. source and data structure of HashMap

    image
    http://www.cnblogs.com/chenssy/p/3521565.html

  9. how to correctly override hashCode()?

    • identical 恒等
    • fast and meaningful 考虑查找效率
    • in an even distribution of values 分布平均
  10. rehashing

performance test

list performance

set performance

map performance

I/O

  1. FileChannel,xxStream.getChannel, ByteBuffer -> asXXBuffer(view buffer), Charset
  2. clear/flip/rewind
  3. nio: MappedByteBuffer
  4. compression
  5. decorator pattern

enums

  1. static import
  2. the mystery of values(): static method added by the compiler
  3. Class.getEnumConstants()
  4. EnumSet are built on top of longs which is 64bit; it's ok when it contains more than 64 enums , think why
  5. < 64 RegularEnumSet; >= 64 JumboEnumSet
  6. EnumMap
  7. command design pattern using enums is so fantastic
  8. chain of responsibility pattern
  9. state machine
  10. 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

  1. it seems counterintuitive that multithreaded program needs context switching cost
  2. concurrency imposes costs, including complexity costs, but these are usually outweighted by improvements in program design, resource balancing, and use conveniece
  3. interface: Runnable vs Callable
  4. enum TimeUnit
  5. ExecutorService exec = Executors.newCachedThreadPools / newFixedThreadPool
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler)
  1. Executors
  2. Future FutureTask
  3. Sleeping: TimeUnit.MILLISECONDS.sleep(X);
  4. daemon thread, gc is a daemon thread in jvm?
  5. how Executors propagate exception
  6. getUncaughtExceptionHandler()
  7. volatile
  8. atomicity applies simple operation on primitive types except for long and double which are two seperate 32-bit operations
  9. volatile ensures visibility across application, as soon as a write occurs for that field, all reads will see the change
  10. java's increment a++ is not atomitic ,it involve both a read and a write
  11. Thread local storage, ThreadLocal
  12. BlockingQueue、CountDownLatch、CyclicBarrier、Semaphore

performance tune

  1. synchronized -> lock / atmoic
  2. lock-free container: CopyOnWriteArrayList ,ConcurrentHashMap
  3. ReadWriteLock
  4. Active Object, actors

task:

  1. wirte a general task util simplifying thread operations
  2. how jvm lock ja
  3. Lock, ReentranceLock, ReadWriteLock,Condition
  4. long vs double

THEN

  1. JDK: collection, exception,reflect.proxy, concurrency, nio...etc
  2. 刷题: codewars 基础部分(8kyu) 快速熟悉起来
  3. 《Effective Java》
  4. 《数据结构与算法 java语言描述》 内功 不解释
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容