2008年7月28日 星期一

JAVA import & package

import最後面一欄要到Class
或上面的Package.*
import static最後一欄則是到static的member

如果不import就要用class
每次用這個class就得packagename.classname

要把package P; 的class檔案compile 出來
要java -d . XX.java
則XX.class會產生到目前資料夾下的P資料夾

執行package為utility.myapp中的Cool.class (放在C:\java\Myclasses\utility\myapp中)
指令 -> java -classpath C:\java\Myclasses utility.myapp.Cool

設定classpath 時 ,舊的會被蓋掉?

2008年7月20日 星期日

JAVA I/O

File:
檔案的一些操作
判斷path是檔案還是資料夾
判斷存不存在

InputStream & OutputStream:
用來輸入輸出byte steam
分別有read()與write()來處理資料,得放在try catch中
int read()會return read到的值
而int read(byte[])則會把結果塞到byte[]中,並return read了幾個byte

Reader & Writer
用來輸入輸出character stream

FileInputStream & FileOutputStream
用來開啟檔案
available()可以用來看有幾個byte可以讀
FileOutputStream constructor的第二個引數是true則為append模式

FileReader & FileWriter
用來開啟檔案

BufferedInputStream & BufferOutputStream
用Buffer緩衝操作次數
flush()

BufferReader & BufferWriter
用Buffer緩衝操作次數
newline()
readline()

DataInputStream & DataOutputStream
有readInt等一堆針對不同type 的存取function

InputStreamReader & OutputStreamWriter
byte與char間的轉換
OutputStreamWriter outWriter = new OutputStreamWriter(new FileOutputStream ("a.txt"),"ISO2022CN");

PrintStream:
System.in是一個InputStream,不可單獨使用
System.out是一個PrintStream,可單獨使用

ObjectOutputStram & ObjectInputStream
要存的物件要implement Serializable
要有private static final long serialVersionUID=1;
trasient修飾的varable不會存起來

2008年7月17日 星期四

JAVA autoboxing & warpper class

若Integer的值在-128~127之間
物件會重覆使用,不過用new的話,還是會產生新的物件

覆載時autoboxing的順序
1. 允許向上轉型(int會找long、float或double)
2. 用autoboxing
3. 找varags自動變長參數

因為int 可以轉為 double
可是Integer不能轉為Double
所以Integer可以轉為double (Integer轉int再轉double)
但int不能轉為Double (int 轉Integer不能再轉Double)

字串轉數字
int x = Integer.parseInt("123");

數字轉字串
String s = String.valueOf(1);

Integer type不會自動initial (因為是物件)

泛型之間是互不相容,不用看繼承關係
反而泛型跟沒泛型的可以相容

2008年7月12日 星期六

JAVA collection

Collection、Map、Set、SortedSet、 List、SortedMap皆為interface
其他的才是class

Collection:
collection下有list與set
Set只有unique的元素
List則可以重複
Collection內存的是Object,讀出來前要先轉型,所以才有generic這種東西

Map:
而Map則是有Set的Key與Collection的value的組合

Set:
有HashSet class
而HashSet下還有LinkedHashSet class,會照add順序排列
HashSet在加入物件時
會先檢查物件的hashCode()是否跟HashSet中的元素有一樣
若一樣,再檢查是否reference到同個物件
若不是,再檢查equals()是否為true

List:
List是有Ordered的,可允許重複元素,有index
下有ArrayList、LinkedList、Vector
ArrayList跟Vector都是Random Access的,差別Vector為thread safe版
LinkedList則是Insert快,是double linked list
List要用contain的話要覆寫equals()

Map:
有key與value,key是unique的,所以是set
Map下有HashMap、LinkedHashMap、HashTable
HashTable是thread safe版的Hash Map,且不允許null Key

equals() :
通常寫法:
1.用==來判斷是否指到同個物件
2.判斷是否同個class
3.把obj轉型為這個class
4.用成員函數是否相等來判斷
Reflexive: x.equals(x) is always true.
Symmetric: F x.equals(y) <-> y.equals(x)
Transitive: if both x.equals(y) and y.equals(z) are true, then x.equals(z) is true.
Consistent: 不管算幾次都是一樣結果
null comparison: For any non-null reference obj, obj.equals(null) is always false.




hashCode()
有覆寫equals()的話也請覆寫hashCode()
eauals()為true的話,hashCode()值一定要一樣
eauals()為false的話,hashCode()值不一定要不同
一般性的規定
Consistency during execution:
Object value equality implies hash value equality:
Object value inequality places no restrictions on the hash value:

SortedMap、SortedSet
只有TreeMap與TreeSet
Implementing Comparable interface的物件並覆寫 compareTo()才可以加進去
*若compareTo() return 0的話,add就沒有作用

PriorityQueue預設自然排序,poll會從head來poll

Collection裡面只能放物件,所以它的generics也只能放物件

HashSet s的元素若要sort,可以把s丟進TreeSet的constructor
s= new TreeSet(s);

JAVA enum

內部enum才可以宣告為static(implict final)
*enum的constructor為private
enum的內容值為public static final

enum的內容其實是String

values()這個method可以return一個element array

JAVA 繼承及物件導向

在Father a = new Son()時
a.varable會call Father的
a.method會call Son的
而如果call static method,則call Father的
而由於a名義上是Father,如果a的method有throws的話
call a.method還是需要用try包起來
可是實際上run time才是去執行Son的method

implement 兩個interface時的語法 implement A, B

interface可以多重extends,class不行

同時extends和implement時要先extends再implement

final method不能被override

JAVA 雜

系統變數
使用指令java -Dxxxx.xxxx = yyyyy
可把xxxx.xxxx這個系統變數加進去,其值為yyyyy
可用System.getProperty("xxxx.xxxx")
或System.getProperties().getProperty("xxxx.xxxx")
來取值

println一個object其實是call它的toString()

DateFormate:
Date date = new Date()用來產生Date物件
Date(int a)則可以產生自1970年過了a微秒後的Date物件
DateFormat shortFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
System.out.println(shortFormat.format(date));
DateFormat則用來將date轉為文字
也可用DateFormat的parse()將String轉為Date
DateFormat.getDateInstance(DateFormat.LONG, Locale.TAIWAN).parse("2005年7月22日");

String、StringBuffer、 StringBuilder
String是immutable的
沒有用new來allocate的話,兩個相同內容的字串會因為有維護string pool而指到同一個object
不過"a"+"b"則不會等於"ab"
*有+ 和 concat來合併字串,不過要記得assign給新的ref
String長度用length()來查 容積是capacity()
P.S. array長度則是用length來查
collection的大小是size() 容積是capacity()
StringBuilder用append來合併字串
還提供String substring()來傳回子字串
*因為是return String,所以也一定要有ref接
StringBuffer是thread-safe版的StringBuilder

Garbege collection
建議VM進行GC的方法
System.gc()
Runtime.getRuntime().gc()

邏輯運算有分short circuit(|| &&) 與一般的版本(| & )
short circuit先做前面,若已能確定,就不會再做後面
一般的版本則會依序做前面後面

*Java的Function call是call by value
傳進去後的改變並不會影響Function外
所以傳ref進去再new會有問題

final的變數一定要手動給定初始值,之後就不能再改了

2008年7月1日 星期二

JAVA thread

Daemon threads 在main thread結束時,也會跟著結束

method synchronized 只對 non-static member有用

static yield: 讓出cpu(可是面對不同priority,可能會沒用)

static sleep (時間) throws Interrupted Exception
有block,所以要用try & catch包

final join (時間) throws Interrupted Exception
有block,所以要用try & catch包
直到目標Thread跑完,才會解除,但目標Thread還是可以把CPU讓給第三者

wait() & notify()則是屬於物件的function,不是Thread的
wait()會交出lock,可是notify()不會,要等Thread結束才會交出

volatide: 在multithread時,確定多個Thread會access到值本身而不是copy

synchronized可以修飾 method
或用synchronized(obj){}來包住區段

final變數不需要考慮同步的問題