Pattern & Matcher
正規表示法
. * [ABC] [A-C]
/d /D /s /S 這些在用的時候要多加個/
也就是//d //D ......
Pattern ptn = Pattern.compile("JAVA [EMS]{2}.*");
Matcher mch = ptn.matcher("JAVA SE 5.0");
matches()可以return是否有完整match
用find()可以return是否字串中有符合的片段
有的話可以用start()與end()來return起始位置與結束位置+1
replaceAll(String str)來把裡面所有符合表示法的片段換為str
並return結果字串
而不必搭配Matcher的split()
可以利用String[] a = ptn.split(str)
來將str這個字串,用正規表示法指出的片段來分割
Formatter
Formatter f = new Formatter(System.out)可以用來獲得標準輸出
而f.format("")可以用來輸出
若constructor內餵StringBuffer則可以輸出到StringBuffer
這情況下f.format()此時會變為append的方法來加入字串
若constructor內餵File("xxx.xxx")
則會輸出到這個檔案 (這個狀況下要用try-catch)
且要用close()
%b boolean %s String %h 16進位hashcode
%c Character %d Integer %o 8進位Integer %x 16進位Integer
%e 浮點數的指數表示法 %f浮點數一般表示法 %g 依精確度格式化的浮點數
%a 16進位指數表示法的浮點數
Scanner
Scanner sc = new Scanner(System.in);
可以用來獲得鍵盤輸入
sc.next() 可以獲得字串輸入
sc.nextInt()可以獲得Integer輸入
2008年8月13日 星期三
JAVA Generics(泛型)
對於多個不同的class
若基本的邏輯相同,可利用泛型,只定義一個class
public class GenericFoo <T> {
private T foo;
public void setFoo(T foo) {this.foo = foo; }
public T getFoo() {return foo;}
}
也能用public class ListGenericFoo <T extends List>
來限制T只能是number的子類別
然而<?>與<? extends number>則是用在變數或參數的宣告
限制這變數只能裝載這些限定的型別
*不過使用?宣告的reference不能用來加入新的資訊
只能取得它的資訊或是移除它的資訊
若基本的邏輯相同,可利用泛型,只定義一個class
public class GenericFoo <T> {
private T foo;
public void setFoo(T foo) {this.foo = foo; }
public T getFoo() {return foo;}
}
也能用public class ListGenericFoo <T extends List>
來限制T只能是number的子類別
然而<?>與<? extends number>則是用在變數或參數的宣告
限制這變數只能裝載這些限定的型別
*不過使用?宣告的reference不能用來加入新的資訊
只能取得它的資訊或是移除它的資訊
Java Exception
Runtime Exception不需要用try catch包起來
override父類別的method時,原method若有throws
新的method可以throws原exception範圍內的exception
或不throws
override父類別的method時,原method若有throws
新的method可以throws原exception範圍內的exception
或不throws
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 時 ,舊的會被蓋掉?
或上面的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不會存起來
檔案的一些操作
判斷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 (因為是物件)
泛型之間是互不相容,不用看繼承關係
反而泛型跟沒泛型的可以相容
物件會重覆使用,不過用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);
其他的才是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 繼承及物件導向
在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
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的變數一定要手動給定初始值,之後就不能再改了
使用指令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變數不需要考慮同步的問題
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變數不需要考慮同步的問題
2008年6月28日 星期六
JAVA inner class
static inner class只能存取enclosing它class內的static成員
static inner class只能放在top level class或 其他static inner class
static inner class裡面可以有static成員及non-static成員
non-static inner class裡面除了final static以外,不能有static成員
non-static inner class可以有任何的accessibility modifier
local inner class 只能存取 包含它的Method內的final變數
如果包含local inner class的context或method是static,則class也是隱含static
local inner class的reference不能在context外面declare,只能用它的superclass來接在context裡面new出來的instance
anonymous inner class 裡只有non-static members 和 final static fields
static inner class只能放在top level class或 其他static inner class
static inner class裡面可以有static成員及non-static成員
non-static inner class裡面除了final static以外,不能有static成員
non-static inner class可以有任何的accessibility modifier
local inner class 只能存取 包含它的Method內的final變數
如果包含local inner class的context或method是static,則class也是隱含static
local inner class的reference不能在context外面declare,只能用它的superclass來接在context裡面new出來的instance
anonymous inner class 裡只有non-static members 和 final static fields
2008年6月24日 星期二
JAVA interface
class可以不實作interface的所有方法,不過要標為abstract
interface的變數隱含 public, static and final且一定要initial值
*而interface的method隱含public
所以要override他一定要設為public
interface的變數隱含 public, static and final且一定要initial值
*而interface的method隱含public
所以要override他一定要設為public
JAVA constructor & initial block
constructor沒有return value (連void都沒有)
有的話,就算是同名,也不是constructor
constructor可以為private或任何modifier
不過若為private,則在外面就不能用這個constructor
constructor裡面
this()或super()只能放在第一行
而且不能同時出現在同一個constructor
在有父類別的情況下
如果兩個都沒有,compiler會偷偷插入一個super()在最前面
這時如果父類別沒有default constructor就會錯誤!
constructor不能access static member?
initial block:
static的順序
照著放置先後順序
instance block順序
super() -> block 或express 照先後順序 -> 其他constructor
有的話,就算是同名,也不是constructor
constructor可以為private或任何modifier
不過若為private,則在外面就不能用這個constructor
constructor裡面
this()或super()只能放在第一行
而且不能同時出現在同一個constructor
在有父類別的情況下
如果兩個都沒有,compiler會偷偷插入一個super()在最前面
這時如果父類別沒有default constructor就會錯誤!
constructor不能access static member?
initial block:
static的順序
照著放置先後順序
instance block順序
super() -> block 或express 照先後順序 -> 其他constructor
訂閱:
文章 (Atom)