初始化

* All class-level (member) variables are initialized before they can
  be used.
  All local variables are not initialized until it is done explicitly.


* 所有的主成員在他們使用之前被初始化
  所有的局部變量必須通過顯式的賦值來初始化

* An array object (as distinct from reference) is always initialized
  (with zeroes or nulls)

* 數組對象總是能夠初始化(零或者null)

* Member initialization with the declaration has exception problems:
- cannot call methods that throw a checked exception.
- cannot do error recovery from runtime exceptions.
- If you need to deal with errors you can put the initialization code
  along with try/catch statements in either a ctor (for instance fields)
  or in a static initialization block for static fields. You can also have
  instance (non-static) initialization blocks but ctors are more
  recognizable.

* 需要處理異常的成員初始化
- 不能調用會拋出異常的方法
- 不能對基本異常做任何處理
- 如果你需要處理錯誤,將初始化的代碼放到構造器或者靜態初始化塊的
  try/catch塊中,當然,你也可以放到非靜態的代碼塊中,但是構造器似乎更為通用。

------------------------------------------------------------------------

Strings

字符串

* The String class
  - Because string is an immutable class, its instance methods that
    look like they would transform the object they are invoked upon,
    do not alter the object and instead return new String objects.
  - String has methods concat(String),trim(),replace(char,char)
  - String has static valueOf methods for a whole bunch of primitives
    and for Object too (equivalent to Object.toString()).
  - in substring(int,int), the second arg is exclusive.
  - indexOf methods returns -1 for 'not found'

* 類String
  - 類String是不可變的,即使他的某些方法看起來會改變字符串的內容,但實際
    上他們返回的是一個新的字符串,而不是改變原來的字符串
  - 類String的方法:cancat(String),trim(),replace(char,char)
  - 類String的靜態方法valueOf能處理所有的基本類型和對象(調用對象的
    toString()方法)
  - 在substring(int,int)方法中,第二個參數是"不包括"的(譯者注:第一個參
    數是"包括"的,例如substring(1,4)將會返回字符串從第二個字符開始(包括
    第二個字符),到第五個字符結束(不包括第五個字符)的子字符串)
  - 如果沒有找到,indexOf方法將返回-1

* String Pool:
  A JVM has a string pool where it keeps at most one object of any
  String. String literals always refer to an object in the string
  pool. String objects created with the new operator do not refer to
  objects in the string pool but can be made to using String's intern()
  method. Two String references to 'equal' strings in the string pool
  will be '=='.

* 字符串池
  虛擬機有一個字符串池,保存著幾乎所有的字符串對象。字符串表達式總是指向
  字符串池中的一個對象。使用new操作創建的字符串對象不指向字符串池中的對象
  但是可以使用intern方法使其指向字符串池中的對象(譯者注:如果池中已經有
  相同的字符串--使用equals方法確定,則直接返回池中的字符串,否則先將字符串
  添加到池中,再返回)。池中兩個相等的字符串如果使用'=='來比較將返回真

* StringBuffer doesn't override equals.

* 類StringBuffer沒有覆蓋equals方法

------------------------------------------------------------------------

Arrays

數組

* Arrays are objects .. the following create a reference for an int array.
    int[] ii;
    int ii[];

* 數組是一個對象 .. 下面的代碼創建一個整型數組的引用:
    int[] ii;
    int ii[];

* You can create an array object with new or an explicit initializer:
    ii = new int[3];
    ii = new int[] { 1,2,3 };
    int[] ii = { 1,2,3 ); // only when you declare the reference.

* 你可以通過new操作或者顯式的初始化創建一個數組對象:
    ii = new int[3];
    ii = new int[] { 1,2,3 };
    int[] ii = { 1,2,3 }; // 只有聲明的時候

* CAREFUL: You can't create an array object with:
    int iA[3];

* 小心:你不能象下面這樣創建一個數組對象:
        int iA[3];

* If you don't provides values, the elements of obj arrays are
  always initialized to null and those of primitive arrays are

* 如果你不提供初始值,對象數組的元素總是初始化成null,基本類型數組的元素
總是初始化成零

------------------------------------------------------------------------

Primitive Types

基本類型

* Primitive types:
  - short and char are both 2 bytes.
    int and float are both 4 bytes.
    long and double are both 8 bytes.
  - char is the only unsigned primitive type.

* 基本類型:
  - short和char的長度是兩個字節。
        int和float的長度都是四個字節。
        long和double的長度都是八個字節。
  - char是唯一的無符號基本類型

* Literals:
  - You can have boolean, char, int, long, float, double and String
    literals.
    You cannot have byte or short literals.
  - char literals: 'd' '\u0c20' (the 0c20 must be a 4-digit hex number).
  - int literals: 0x3c0 is hex, 010 is octal(for 8).
  - You can initialize byte, short and char variables with int literals
    (or const int expressions) provided the int is in the appropriate range.

* 表達式
- 只有boolean,char,int,long,float,double和字符串的表達式;沒有byte
  和short的表達式
- 字符(char)表達式:'d'、'\u0c20'(0c20必須是四位的十六進制數字)
- 整型(int)表達式:0x3c0是十六進制形式,010是八進制形式
- 可是使用合法范圍內的整型表達式對byte、short和char變量初始化

* CAREFUL: can't assign a double literal to a float .. float fff = 26.55;

* 小心:不能將一個double表達式賦給一個float變量 .. float fff = 26.55;

* The only bit operators allowed for booleans are &^| (cant do ~ or

* 位運算只有&^|(不能使用~或者移位操作)

* Primitive wrapper classes
   - are immutable.
   - override equals.
   - the static valueOf(String) methods in primitive wrapper classes return
     wrapper objects rather than a primitives.

* 基本類型的包裝類
  - 不可變的
  - 覆蓋equals方法
  - 靜態方法valueOf(String)返回的是包裝類而不是基本類型

------------------------------------------------------------------------

Conversions and Promotions

類型轉換

* boolean->anything but boolean or string is not allowed.
* All other primitive conversions are allowed with an explicit cast.
* char/byte/short/int/long to float/double is a widening conversion even
  if some precision is lost (the overall magnitude is always preserved).
* Narrowing conversions require an explicit cast.
  - integral narrowing conversions simply discard high-order bits.
  - anything to char is a narrowing conversion (inc byte) because its
    signed to unsigned and negative numbers get messed up

* boolean不能跟其它的任何類型相互轉換,但是boolean->String是允許的
* 所有的基本類型之間可以通過顯式的類型轉換而轉變成其它類型
* char/byte/short/int/long到float/double的轉換是寬轉換,即使有可能丟掉部
  分信息
* 窄轉換需要顯式的轉換
  - 整型的窄轉換只簡單的去掉高位比特
  - 所有到char的轉換都是窄轉換(包括byte)因為轉換是從有符號數到無符號數
    的轉換,負數將會得到一個混亂的結果

* Widening primitive and reference conversions are allowed for assignment
  and in matching the arguments to a method (or ctor) call.

* 對象和基本類型的寬轉換允許在賦值和匹配的方法調用中(非顯式的)使用

* For assignment (but not method invocation), representable constant
  int expressions can be converted to byte, char or shorts (eg. char c =
  65).

* 賦值時,合法的整型表達式能被自動轉換成byte、char或者short(例如:
  char c = 65)

* Unary numeric promotions: byte/short/char to int

* 一元運算時,byte/short/char將自動轉換成int

* Binary numeric promotions:
  - both arguments are made (in order of preference)
    double/float/long/int.
  - include (in)equality operators.

* 二進制數據類型轉換:
  - 所有的參數自動轉換成(按循序的)double/float/long/int
  - 包括比較運算

* char/byte/short are promoted to int for nearly every operator ... be
  careful not to assign the uncast int return value to a narrower type,

     bytesum = byte1 + byte2; // won't compile without a cast.
     bytesum += byte2; // is ok.
  - applies to bitshift operators (as a unary promotion on each arg).
  - applies to unary + and - (eg. byte b = +anotherByte; needs a cast).

  - there are no promotions for ++, --, += etc.

        byte b=10;
        char c = b++; //explicit cast needed to convert byte to char
        char c = b++ +10; //explicit cast needed to convert int to char
        char c = b++ +10L; //explicit cast needed to convert long to char

* char/byte/short幾乎在所的運算中都被轉換成int … 要當心不要將int類型的
  返回值在沒有顯式轉換之前賦給一個更小的類型:
        bytesum = byte1 + byte2; //沒有顯式的轉換不能通過編譯
        bytesum += byte2; // 沒有問題
  - 本規則適合于位運算(以及每個一元運算的參數)
  - 本規則不適用于++,--,+=等操作

        byte b = 10;
        char c = b++; //需要顯式的將byte轉換成char
        char c = b++ + 10; //需要顯式的將int轉換成char
        char c = b++ + 10L; //需要顯式的將long轉換成char

* A switch argument can be any type that can implicit-cast to an int
  (byte/char/short/int but not boolean or long).
  - The argument and cases can also be compile-time constant
    expressions.

* switch的參數可以是任何可以自動轉換成int型的基本類型(
  byte/char/short/int是合法的參數,但是boolean和long型是不合法的)
  - switch的參數和case的參數可以是常量表達式

* Explicit Casting:
  - Impossible casts are detected at compile time.
    Other bad casts cause runtime exceptions rather than messes.

* 顯式的轉換
  - 不可能的轉載編譯期間就能檢測到。其他錯誤的轉換將拋出異常以防止數據的
混亂

* Array casts:
  - The only implicit conversion for arrays is: Base[] base = new Der[5];
    - a runtime exception is thrown if you try to add anything but Derived
  - There are no implicit casts for arrays of primitives.
  - You can make an explicit array cast:
         String[] strs = (String[]) ObjectArray;


* 數組轉換:
  - 對象數組唯一的自動轉換的情況是:Base[] base = new Der[5];
    - 如果你嘗試添加Derived以外的對象,將會得到一個運行期異常
  - 基本類型數組沒有自動轉換的情況
  - 可以使用顯式的數組類型轉換:
                String[] strs = (String[]) ObjectArray;

------------------------------------------------------------------------

------------------------------------------------------------------------

Object-oriented concepts

面向對象的概念

* The signature of a method is its name, argument type and argument order.
  - an overload is legal provided the two methods have different signatures.
  - an override must have the same signature and return type, throw no new
    checked exceptions and be at least as accessible as the method it is
    overriding.

* 一個方法通過它的名字,參數類型以及順序來標識
  - 有不同標識的方法重載是合法的
  - 方法覆蓋必須有相同的標識和返回值,不能拋出新的普通異常,不能比他要覆
    蓋的方法擁有更少的權限

* Fields do not act polymorphically:
  - whenever you access a field from outside the class, the declared type of
    the reference is used rather than the type of the object it refers to.
  - regardless of the type of the reference, a method will always access its
    own fields rather than those of a derived class.

* 變量成員沒有多態性
  - 當你從類外面引用變量成員時,你總是得到定義的類型,而非實際指向的類型

  - 不管變量成員的類型是什么,方法總是使用類自己的變量成員,而不是其他的
    繼承過來的變量成員

* Private method calls are statically bound ... a method will call
  the private method in the class where it is defined .. even if the
  calling method is inherited by a derived class and the derived
  class defines a method with the same signature as the private method.


* 私有方法的調用是靜態綁定的 … 方法可以調用他所在類之內的私有方法 .. 即
  使調用的方法的夫類中有與此私有方法標識相同的方法

* Calls to public and protected methods in the same class are dynamically
  bound ... even for constructors (and from private methods). This is
  different to C++.

* 對公共的和保護的方法的調用是動態綁定的 … 同樣構造函數(不同于私有方法?)
  也是動態綁定的。這是與C++不同的地方

* Re-using a static method's name in a derived class:
  - A static method cannot be overriden by a non-static method
  - A static method can be overriden by a static method but does not
    dynamically bind (therefore static methods can't be abstract)
  - You can overload a static method with a non-static method.
  * note also that a static method can be inherited.
  * note that a static var can be 'overriden' by a non-static var.

* 重用夫類中的靜態方法的名字:
  - 靜態方法不能被非靜態的方法覆蓋
  - 靜態方法可以被靜態方法覆蓋,但是不能動態綁定(所以靜態方不能是抽象的)
  - 可以使用非靜態的方法重載靜態的方法
  * 注意靜態方法是可以繼承的
  * 注意靜態的變量可以被非靜態的變量"覆蓋"

* It's legal to assign any reference type including an Interface reference
  to an Object reference .. which makes sense because the interface ref
  must point to an Object (or null).

* 講任何類型的引用包括接口的引用付給一個Object的引用是合法的 .. 應為一個
  接口的引用總是指向一個Object(或者null)

------------------------------------------------------------------------

Nested classes

-> from the Java Language Specification
-> from Sun's Java Tutorial

嵌套類

-> 來自Java語言規范
-> 來自Sun的Java教程

*** A nested class is a class that is defined inside another class.

*** 嵌套類是定義在另外一個類的內部的類

* There are two distinct types of nested classes:
  - static nested classes (or top-level nested classes)
  - inner classes (which are always associated with an instance of
    an enclosing class).

* 有兩種截然不同的嵌套類
  - 靜態的嵌套類(或者說頂層的嵌套類)
  - 內部類(總是定義在其他類的實例里面)

* (Nested) inner class types:
  - member classes,
  - local classes (method or code block),
  - anonymous classes.

* (嵌套的)內部類:
  - 成員類
  - 局部類(方法或者代碼塊)
  - 匿名類

* Top-level classes (all classes are either top-level or inner)
  - static nested classes,
  - package member classes.

* 頂層類(所有的類不是頂層類就是內部類)
  - 靜態的嵌套類
  - 包成員類

* Access to enclosing class:
  - all outer-class members (inc. private) are accessible to an inner
    class [Usually without scope modifiers, but if they are hidden by
    an inner class name, you can use Outer.this.outerVar]
  - static nested classes cannot acccess instance variables from enclosing
    classes (there is no instance), but can access their static variables
    (and classes i would think).

* 內部類的權限
  - 外部類所有的成員(比如私有的)都有權限訪問內部類 [通常沒有任何的權限
    修飾,但是如果對內部類是隱藏的,可以通過Outer.this.outerVar來引用]
  - 靜態內部累不可以引用外部類的實例變量(沒有實例),但是可以引用外部類
    的靜態變量(我想,引用靜態的類也是允許的)

* Instantiation:
  - For accessible inner classes: Outer.Inner i = new Outer().new Inner();
    Even if you are in Outer's scope, you need to have an Outer instance.
  - For accessible static nested classes:
    Outer.Nested nested = new Outer.Nested();

* 實例化:
  - 內部類的引用:Outer.Inner i = new Outer().new Inner();
    即使在外部類的范圍內,你也需要一個外部類的實例
  - 靜態內部類的引用:Outer.Nested nested = new Outer.Nested();

* Local inner classes cannot access non-final local variables or method
  arguments.

* 局部內部類不能引用非最終(final)的局部變量和方法參數

* Nested classes generally have the same options with regard to
  modifiers as do variables declared in the same place.

* 嵌套類可以與同等位置上的變量擁有相同的權限修飾

* Unlike class-level nested classes, local classes are executed in the
  method's sequence of execution so you can't create an instance of the
  local class before it is declared.

* 與類一級的內部類不同的是,局部類是按次序執行的,所以你不能在定義之前創
  建一個類的實例

* The static keyword marks a top-level construct (class, method or
  field) and can never be subject to an enclosing instance.
  - no inner class can have a static member.
  - no method can have a static member.

* 關鍵字static使一個頂層的構造(類,方法或者成員變量)不屬于他的外部類實例
  - 內部類不能有靜態成員
  - 方法不能有靜態成員

* Interfaces automatically attach 'public static final' to field and
  class members (thus making them top-level nested rather than member
  inner classes).

* 接口自動將"public static final"的權限修飾賦予所有的成員變量和方法(這
  將保證它們是頂層嵌套的而不是內部成員類)

* A nested class cannot have the same simple name as any of its
  enclosing classes (note: there is no similar restriction on method
  or variable names).

* 嵌套類不能使用與它的任何外部類的名字相同的名字來命名(注意:方法和變量

------------------------------------------------------------------------

------------------------------------------------------------------------

Threads

線程

* Know where the basic thread methods are:
  - wait, notify and notifyAll are Object instance methods.
  - start, stop, suspend, resume and interrupt are Thread instance methods.
  - sleep and yield are Thread static methods

* 了解基本的線程方法的位置:
  - wait,notify和notifyAll是Object的實例方法
  - start,stop,suspend,resume和interrupt是Thread的實例方法
  - sleep和yield是Thread的靜態方法

* Thread states:
  - Bruce Eckel lists 4 thread states: new, runnable, blocked, dead.

* 線程的狀態
  - Bruce Eckel 列出四種線程狀態:創建,可運行的,堵塞的,死亡

* Blocking
  - There are 5 ways a thread can be blocked - sleep, wait, suspend,
    synchronization, io blocking.
  - sleep and suspend do not release locks held by the thread.

* 堵塞的
  - 有五種方法可以使一個線程堵塞 - sleep,wait,suspend,同步,io堵塞
  - sleep和suspend期間線程不釋放對象的鎖

* Deprecated methods
  - stop is unsafe because it releases all locks and may leave objects in
    an inconsistent state.
  - suspend is deprecated because its failure to release locks makes it
    prone to deadlock. Calling wait in a sync block is safer.

* 不推薦使用的方法
  - stop方法因為釋放所有的鎖而導致有可能使線程進入不一致的狀態,不安全
  - suspend不推薦使用是因為它不能釋放鎖而導致有可能進入死鎖狀態。在同步

* The isAlive method returns false for new threads as well as dead threads.

* isAlive方法在線程創建和死亡的狀態下都返回false

* Threads inherit their priority from the thread that calls their ctor.

* 線程繼承調用他們的構造函數的線程的優先級

------------------------------------------------------------------------

Exceptions

異常

* Non-runtime exceptions are called checked exceptions.

* 非運行期異常叫普通異常

* Even if a method explicitly throws a runtime exception, there is no
  obligation for the caller to acknowledge the exception. One consequence
  of this is that the restriction on exceptions thrown by an overriding
  method only applies to checked exceptions.

* 如果方法拋出運行期異常,調用者沒有必要知道。由此推論出,方法覆蓋拋出異
常的限制只適用于普通異常

* A try block's finally clause is called unless the JVM is exited (i think).
  - a return in try or catch does not prevent finally from being executed.

* try塊的fanally字句總是被執行,除非程序推出虛擬機(我想是這樣的)
  - try或者catch里面的語句不能阻止finally字句的執行

* A try block's finally statement will be executed (unless the thread dies)
  before control leaves the try/catch/finally scope. It will be executed
  before unhandled exceptions (from try or catch) are passed back up the
  calling stack.

* try塊的finally字句在退出try/catch/finally之前執行(除非線程已經死亡)。
  它將會在將沒有捕捉的異常(產生于try或者catch)送回調用堆棧之前執行

* If you return from a try and finally does not return ... 1) the return
  value is calculated, 2) finally executes and 3) the method returns with

* 如果你從try字句返回并且finally字句不包含返回 … 1) 計算返回值,2) 執行
  finally字句, 3) 方法返回執行finally之前計算的結果

* If you have a return in both try and finally, the finally's value
  is always returned.

* 如果你同時在try和finally中返回,則總是返回finally中的返回值

* If try/catch excludes continuation following finally, it is a compile
  error to have any statements there.

* 在try/catch和finally之間放置任何語句將會導致編譯錯誤

* Primitive floating point operations do not throw exceptions. They use
  NaN and infinity instead.

* 基本的浮點運算不會拋出異常,它們使用NaN和infinity來表示異常的結果

* A constructor can throw any exception.


------------------------------------------------------------------------

Streams



* System.in is an InputStream and out and err are PrintStreams.

* System.in是一個InputStream,out和err是PrintStream

* Beware of instances of the abstract OutputStream and InputStream.

* 慎防實例化抽象的OutputStream和InputStream

* Some OutputStreams:
  OutputStream
    - write(int) writes the eight low-order bits to the underlying stream
    - write(byte[]) write(byte[],int off,int len)
    - flush()
    - close()
  BufferedOutputStream
    - has two ctors: (OutputStream) (OutpuStream, int size)
  DataOutputStream
    - writes primitives and strings to a byte-based stream.
  ObjectOutputStream
    - writes primitives, strings and serializable objects (inc arrays).
    - is not a FilterOutputStream (seems strange).
  PrintStream
    - two ctors: (OutputStream) (OutputStream, boolean autoflush)
    - never throws IOExceptions (sets internal flag instead)
    - print and println for primitives and strings.
    - print(Object) prints Object.toString().
    - System.out and System.err are printstreams.
  FileOutputStream
    - 4 constructors: (File) (FileDescriptor) (String)
      (String, boolean append)
  PipedOutputStream
    - two ctors: () (PipedInputStream)
    - method to connect(PipedInputStream)
  ByteArrayOutputStream
    - 2 ctors: () (int size)

* 一些OutputStream:
  OutputStream
    - write(int) 將int的低八位寫入底層的流
    - write(byte[]) write(byte[],int off,int len)
    - flush()
    - close()
  BufferedOutputStream
    - 兩個構造函數:(OutputStream) (OutpuStream, int size)
  DataOutputStream
    - 將基本類型和字符串寫入基于字節的流
  ObjectOutputStream
    - 寫入基本類型,字符串和串行化的對象(例如數組)
    - is not a FilterOutputStream (seems strange).
  PrintStream
    - 兩個構造函數:(OutputStream) (OutputStream, boolean autoflush)
    - 永遠不會拋出IOException(取而代之的是設置某些標記位)
    - print and println的參數可以是基本類型和字符串
    - print(Object)打印Object.toString().
    - System.out和System.err是PrintStream
  FileOutputStream
    - 四個構造函數:(File) (FileDescriptor) (String)(String, boolean append)
  PipedOutputStream
    - 兩個構造函數:() (PipedInputStream)
    - 方法connect(PipedInputStream)
  ByteArrayOutputStream
    - 兩個構造函數:() (int size)

* Some Writers
  - OutputStreamWriter
  - StringWriter, CharArrayWriter - like ByteArrayOutputStream
  - FileWriter, BufferedWriter, PrintWriter, FilterWriter
  - There is no ObjectWriter, DataWriter.
  - PrintWriter can be constructed with an OutputStream or Writer.

* 一些Writer
  - OutputStreamWriter
  - StringWriter, CharArrayWriter - 與ByteArrayOutputStream相似
  - FileWriter, BufferedWriter, PrintWriter, FilterWriter
  - 沒有ObjectWriter, DataWriter.
  - PrintWriter可以使用OutputStream或者Writer來構造

* Some Readers:
  - LineNumberReader doesn't attach numbers, it has getLineNumber().
  - CharArrayReader is ctd with the char[] that it reads from and

* 一些Reader:
  - LineNumberReader并不會附加行號,可以通過getLineNumber()方法取得行號
  - CharArrayReader使用char[]來構造,從char[]中讀取數據,參數int start和
    length是可選的

* RandomAccessFile
  - does not extend File or any type of stream.
  - two ctors: ( File or String name, String mode="r","rw" )
  - checks for read/write access at construction (unlike File).
  - has read/write methods for primitives and strings (and a couple of
    others).
  - has a (long//mahachanged fm byte to long) file-pointer: seek(long),
    long length().

* RandomAccessFile
  - 并非繼承自File或者任何類型的流
  - 連個構造函數:( File or String name, String mode="r","rw" )
  - 在初始化時檢查文件的讀寫權限(不同于File)
  - 對基本類型和字符串(還有其他的)有專門的讀寫方法
  - 有一個long型的文件指針:seek(long), long length().

* FileDescriptor
  - just a handle to a sink/source of bytes.
  - only has two methods: sync() and valid()

* FileDescriptor
  - 只是作為字節接收器/源的一個句柄
  - 只有兩個方法:sync() and valid()

* File
  - represents an abstract file or dir pathname (file/dir doesnt have
    to exist).
  - 3 ctors: (File or String parent, String child) (String pathname)

* File
  - 對抽象的文件或者目錄的描述(文件/目錄不一定存在)
  - 三個構造函數:(File or String parent, String child) (String pathname)

------------------------------------------------------------------------

Collections


* The Collection interface is extended by Set and List (and SortedSet).
  - a set contains no duplicates.
  - a list is a sequence and can have duplicates.
* The Map interface does not extend Collection and is extended by
  SortedMap.

* Set、List和SortedSet繼承自Collection
  - Set不包含重復的元素
  - List是有序的,可以包含重復的元素
* Map并非Collection的子類,SortedMap繼承自Map

* There are abstract classes for each of the main interfaces (Collection,
  Set, List and Map) and implementations extend these.

* 主要的接口都有相對應的抽象類,具體的實現從抽象類繼承

* Set implementations
  - HashSet
  - TreeSet (implements SortedSet)
* List implementations
  - ArrayList
  - LinkedList
* Map implementations
  - HashMap
  - TreeMap (implements SortedMap)

* 實現Set的類
  - HashSet
  - TreeSet(實現SortedSet)
* 實現List的類
  - ArrayList
  - LinkedList
* 實現Map的類
  - HashMap
  - TreeMap(實現SortedMap)

* Vector and Hashtable (extends Dictionary) are older collection classes.
  - Vector has been made to extend AbstractList and is like a sync'd
    ArrayList (maha:set)z.
  - Hashtable still extends Dictionary but it implements Map.
  - In contrast to other collection types, Vector and Hashtable are
    synchronized.

* Vector和Hashtable(繼承自Dictionary)是較老的集合類
  - Vector繼承自AbstractList,與同步的ArrayList相像
  - Hashtable仍然繼承自Dictionary但是實現了Map接口
  - 與其他集合類型相比較,Vector和Hashtable是同步的

* There are Collections and Arrays classes to provide static methods for
  general algorithms on collections and arrays.

* Collection和Array都提供了靜態的方法處理集合和數組的運算

* Stack extends Vector
  - it has methods push(Object), pop, peek and search(Object).
  - Push is identical to addElement but also returns the added Object.
  - The top of a stack is the last element in the vector (and has index 1 as
    far as the search method is concerned).

* Stack繼承自Vector
  - 方法:push(Object),pop,peek和search(Object)
  - push方法與addElement的效果相似,同時返回剛剛添加過的對象
  - 棧頂是Vector里面最后一個元素(search方法將返回1)


Math methods

Math的方法

* most act on and return double values
 - note that floor(double), ceil(double) and rint(double) return doubles
   not ints

* 大部分的方法都是對double類型數據作處理并返回double型的結果
  - 注意floor(double),ceil(double)和rint(double)返回的是double類型而不
    是int

* abs, max and min can take double, float, int or long arguments and the
  return type matches the argument type.

* abs,max和min方法可以處理double,float,int或者long型的參數并且返回與
  參數類型一樣的返回值

* There are three rounding methods:
  - int round( float ); // note both are 32-bit
  - long round( double ); // note both are 64-bit
  - double rint( double );

* 三個取整方法:
  - int round(float); // 注意兩者都是32位
  - long round(double); // 注意兩者都是64位
  - double rint(double);

* log returns natural log.

* log方法返回自然對數

* trig functions take double arguments in radians.

* trig函數的參數是double型的弧度

------------------------------------------------------------------------

------------------------------------------------------------------------

AWT


* Component is abstract ... Container is not abstract.

* Component是抽象的 … Container不是抽象的

* Checkbox has 5 ctors: no-arg + four with String as first arg and
  combinations of boolean initialState and CheckboxGroup.

* Checkbox有五個構造函數:一個沒有任何參數,其他四個都以字符串為第一個參
  數,以及boolean型的初始化狀態、CheckboxGroup

* CheckboxMenuItem has nothing to do with Checkbox or CheckboxGroup.
  - i think they generate both Item- and ActionEvents.

* CheckboxMenuItem與Checkbox和CheckboxGroup沒有任何關系
  - 我想他們都產生ItemEvent和ActionEvent

* Listeners:
 - InputEvent (super for MouseEvent and KeyEvent) has a 'long getWhen()'
   method.
 - MouseEvent has 'int getX', 'int getY' and 'Point getPoint' methods.
 - ActionEvent and ItemEvent are not ComponentEvents so have to use
   getSource().
 - MenuItems (inc. menus) can produce ActionEvents but not ItemEvents.
 - ActionListener,Adjustment, ItemListener and TextListener only have
   one method and therefore don't have Adapters.
 - KeyListener: Pressed/Released/Typed
 - FocusListener: Lost/Gained
 - ComponentListener: Shown/Hidden   Moved     Resized
 - ContainerListener: component- Added/Removed
 - WindowListener: Opened/Closing/Closed
                   Activated/Deactivated ... keyboard focus.
                   Iconifed/Deiconified
 - MouseListener: Pressed/Released/Clicked
                  Entered/Exited
 - MouseMotionListener: Dragged/Moved

* 接收器:
 - InputEvent(MouseEvent和KeyEvent的父類)有方法:long getWhen()
 - MouseEvent的方法:int getX,int getY,Point getPoint
 - ActionEvent和ItemEvent不屬于ComponentEvent,只能使用getSource()
 - MenuItem(例如菜單)產生的是ActionEvent而不是ItemEvent
 - ActionListener,Adjustment,ItemListener和TextListener只定義了一個方法
   ,所以不需要適配器
 - KeyListener:鍵的按下/釋放/敲擊
 - FocusListener: 焦點的失去/獲得
 - ComponentListener: 顯示/隱藏,移動,改變大小
 - ContainerListener: 添加/刪除組件
 - WindowListener: 打開/關閉中/已經關閉
                   激活/無效 ... 鍵盤焦點
                   最小化/恢復
 - MouseListener: 鍵的按下/釋放/點擊
                  鼠標進入/退出
 - MouseMotionListener: 拖動/移動

------------------------------------------------------------------------

Layout

布局

* BorderLayout is the default layout for Window/Frame/Dialog.
  - add( component, BorderLayout.NORTH ) == add( "North", component )
  - adding a component without an explicit position is identical to
    adding a component at BorderLayout.CENTER.
  - if more than one component is added to the same position, the most
    recently added component is displayed there.
  - north,south,east and west components only expand on one axis.

* BorderLayout是Window/Frame/Dialog的默 喜季 管理器
  - add(component,BorderLayout.NORTH) == add("North",component)
  - 如果不說明位置,組件將按默認的設置添加到CENTER的位置
  - 如果超過一個組件添加到一個位置上,最后添加的將被顯示
  - north,south,east和west上的組件只在一個方向上延伸

* FlowLayout is the default layout for Panels (including Applets).
  - if the panel is bigger than its components, they are centered
    horizontally and at the top (ie. north position).

* Panel(包括Applet)的默 喜季 管理器是FlowLayout
  - 如果panel比組件大,組件將被水平居中并置于頂端

* GridLayout with container larger than components expands to fill its
  container provided that the number of components matches the
  rows*columns.
  - Empty rows are given space, while empty cols are not.
  - If there aren't enough components, it will try to fill its rows first.

* 當GridLayout的容器比組件大時,組件將被擴充到填滿整個容器,需要提供
  rows*columns個組件
  - 空行將被分配空間,但時空列不分配空間
  - 如果組建的數目不夠,首先嘗試填充所有的行

* Ctors for BorderFlow- and GridLayout can take int hgap and vgap args.
  - FlowLayout(int align, hgap, vgap)

* BorderFlow以及GridLayout的構造函數可以帶參數hgap和vgap
  - FlowLayout(int align, hgap, vgap)

* Be careful with nested layout questions ... eg Button to panel to frame,
  the panel fills the whole frame, but the button will be its preferred size
  at north position in the panel (and thus the frame).

* 對嵌套的布局管理器要小心 … 例如一個frame包含一個panel,panel包含一個
  Button,panel填充整個frame,但是button仍然只有預定的大小,位于frame的
  北面

* GridBagLayout
  - There are two ways to set the constraints for a component in a gbl:
      container.add( Component c, Object gbc ) or
      gbl.setConstraints( Component c, GridBagConstraints gbc )
  - weightx and weighty are doubles (0 to maxDouble, default 0) and determine
    where extra width or height is added if a row or column is smaller than
    the container.
  - gridwidth and gridheight are ints (1 to maxInt) ... RELATIVE, REMAINDER.
  - gridx and gridy are ints (0 to maxInt) ... RELATIVE
  - RELATIVE can apply to gridx/gridy (=next) or
    gridwidth/gridheight (=second last)

* GridBagLayout

  - 有兩種方法可以將一個組件使用constraints添加到GridBagLayout中去:
                container.add(Component c,Object gbc) 或者
                gbl.setConstraints(Component c,GridBagConstraints gbc)
  - weightx和weighty是double類型(0到maxDouble,默認是0)的屬性,它們決
    定額外的寬度或者高度是否添加到當前的行或者列
  - gridwidth和gridheight是整型的數據類型(從1到maxInt) … 相對的,剩余
    的
  - gridx和gridy是整型的(從0到maxInt) … 相對的
  - …

* A Component added to a null layout won't display unless you set its
  bounds ... this is the only place where a component can control its
  parent's layout directly.

* 如果將一個組件添加到一個空的布局管理器中,只有設置組件的范圍才能夠顯示

* Illegal arguments (eg. adding a container's parent to the container, adding
  a window) get through the compiler but throw an exception at runtime.


* 不正確的參數(例如,添加一個容器的容器到自身,添加一個window)可以通過
  編譯,但是將會在運行期間拋出異常

------------------------------------------------------------------------

------------------------------------------------------------------------

Miscellaneous

雜錦

* A class type's name is valid as an identifier, eg. int Boolean = 5;
* Modifiers:
  - Automatic variable = local variable = variable declared in a method.
  - Transient fields are not written out when a class is serialized.
  - Transient and Volatile can only be applied to fields.
  - Native can only be applied to methods.
  - You can have a static synchronized method .. it is synchronized on the
    class object.
  - static transient is legal but doesn't effect anything.

* 一個類的名稱是合法的標識名,例如:int Boolean = 5;
* 修飾符:
  - …
  - 當序列化一個類時,以transient修飾的字段不會寫進數據流
  - transient和volatile只能修飾字段
  - native修飾符只能用于方法
  - 可以有同步的靜態方法,它使用類對象作為同步鎖
  - static transient是合法的,但是并不去起作用

* The right-hand-side of an assignment can be a reference to null.
   - You can println a null reference (and add it to another string).
   - What you can't do with a null reference is ... nullRef.aMethod();


* 賦值操作的右側可以是null
  - 可是打印空的引用(還可以賦給一個字符串變量)
  - 你不能做的是 … nullRef.aMethod();

*
        1.      ++,--,unary +,unary -,~,!,()
        2.      *,/,%
        3.      +,-
        4.      >>, >>>, <<
        5.      >, >=, <, <=, instanceof
        6.      == , !=
        7.      &
        8.      ^
        9.      |
        10      &&
        11      ||
        12      ? :
        13      =,+=,-=,*=,/=,%=, &=,^=,|=, >>= ,<<= ,>>>=
* Order of Operation
  - arithmetic before &^|
  - & then ^ then |

* 運算順序
  - 先數學運算,后&^|
  - 先&然后^最后|

* Garbage Collection
  - unreachable objects can become reachable (if their finalize method
    causes another object to have a reference to them) .. but i think
    finalize if guaranteed to not run again.
  - objects referenced by block-level variables are probably not available
    for garbage collection until their enclosing method's scope is exited.

* 內存回收
  - …
  - …

* The compiler never object to creating a local instance of the
  the class being constructed in its constructor. If the call produces
  an infinite loop, a runtime error will occur.

* 編譯器從不反對在構造函數中創建一個本類的實例,但是如果這導致無限的循環
,一個運行期間的錯誤將會被拋出

* Labelled break and continue statements:
  - The label referred to by a labelled break statement is attached to
    a statement block, which could be a loop or switch but doesnt have
    to be.
  - The label referred to by a labelled continue statement must be
    attached to a loop.(A "continue" statement must be enclosed in a "while",
    "do" or "for" statement.)

* 帶標號的break和continue聲明
  - 帶標號的break可用于標識循環和開關語句,但不是必須的
  - 帶標號的continue只能用于循環語句(continue必須出現在while、do或者
    for子句中)

------------------------------------------------------------------------

------------------------------------------------------------------------

Things to look out for

*** Read the answers before going through code samples.


****** WHEN YOU FINISH THE EXAM GO BACK AND CHECK FOR NON-STATIC
       METHODS/VARS FROM MAIN AND OTHER STATIC METHODS

* if (...)
    statement1;
    statement2; //...always executed

* Beware of an otherwise legal override having a more restrictive
  access modifier.


* Beware of missing return statements.

* Look for static modifiers and make sure they don't contain refs to
  instance vars.

* Beware of standard methods (eg. main, paint, run) with the wrong
  args or return type.

* With array declarations, look out for "int intArray[5] = ..."

* Beware of adding a primitive to a Vector.
  - more generally, you can't use a primitive where an Object is
    required (eg. the equals method).

* System.out.println( aReferenceToNull ); // is fine

* Beware of local vars from a try block used in its catch block
  (out of scope).

需要留意的

*** 先讀答案在看代碼段

****** 尋找在main中調用的非靜態的變量和方法

****** 當你完成考試時,回頭檢查main和其他靜態方法中的非靜態變量和方法調用

* if (...)
    statement1;
    statement2; //...總是運行


* 留意沒有返回值的方法

* 保證靜態代碼不能引用類實例變量


* 留意標準方法(例如,main,print和run)的不正確的參數類型和返回值

* 數組聲明,留意形如"int intArray[5] = ..."的語句

* 小心不要將基本類型添加到Vector中
  - 更普遍的情況,當需要用Object的時候不要用基本類型(例如equals方法)

* System.out.println( aReferenceToNull ); // 代碼將運行得很好

* 小心不要在catch中使用try中定義的局部變量(超出范圍)

--
  -= HOVER =-