Java 筆記

1. 一樣的資料,一維陣列和二維陣列所占空間是否相同?
  NO,二維陣列更占空間,例如:int array[100][2],第一個[100]會包含其他size, jvm的資料,因此並不單純是sizeof(int) * 100,會是(sizeof(int) + (data of jvm & size)) * 100

2. finalize() => a function of Object, called before GC release resource

3. transient 關鍵字有何作用?
防止序列化,修飾參考型別,則為null,修飾基本型別,則為0。
若是在不可序列化的類別中,則也該用transient修飾,因為在序列化時會直接跳過

4. Exception & if-else
 a. Avoid throwing exception
https://www.codeproject.com/Tips/490765/If-else-instead-of-try-catch
 b. Checked exception & Unchecked exception
Should we make our exceptions checked or unchecked?
Following is the bottom line from Java documents
If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception
 c. Exception
https://matt33.com/2016/12/13/java-exception/

Generic 泛型
1. 泛型是在compile time的時候進行替換,Runtime時已經決定

要注意,PortalResponse要指名T的型態,否則Java compiler可能會認不出來

需要在使用時,指定型態才行

==========

Collection:
1. Array Collection的轉換
  a. Arrays.asList() 轉換陣列為List
  b. List.toArray(), Set.toArray() 轉換為陣列
2. 父集Collection => 子集Queue, List, Set,Map是另外不屬於Collection
3. Collections是Collection的各種工具集合
  a. 排序sort(), 亂序shuffle()
  b. 尋找極端值, min(), max()

Iterator:
1. Iterator & ListIterator
  a. ListIterator 繼承 Iterator,支援修改/增加 Iterator和檢查List的index
2. Iterator是新的介面,將取代Enumeration

ArrayList:
1. ArrayList和LinkList差異在底層,前者是以Array實現,後者是以Link List實現,優缺點就在兩者的移動,增減有差異
2. ArrayList和Vector差異在thread safe的差異

Queue:

「block queue java」的圖片搜尋結果

1. addXXX(), offerXXX() => 插入 node
2. peekXXX() => 獲得但不移除 node
3. pollXXX() => 獲得並移除 node

Set:
TreeSet 可排序的set
LinkedHashSet 可記錄add步驟的set

Math & Digits:
1. 高精度的數字與浮點數: (效能較差)
BigInteger & BigDecimal
2. 格式化數字結果,DecimalFormat,可搭配BigInteger & BigDecimal,因為這兩者的可讀性較差

InputStream & OutputStream and Files operation
1. DataInputStream / DataOutputStream
2. ObjectInputStream / ObjectOutputStream
3. 建立暫存檔 File.createTempFile()
4. MappedByteBuffer => 用於memory mapping,可以將檔案map到記憶體上