l Utility Classes
l class、method、constructor、Boolean、...都是object。
l System Properties(系統參數)
用途
| 取出或設定系統參數
|
舉例
| java –Deigh=8 Test
println(System.getProperty(“java.version”)); //1.4.2
System.setProperty(“two”,2);
Integer wi = Integer.getInteger(“two”);
println(wi.toString); //2
System.setProperty(“love”,”true”);
boolean b = Boolean.getBoolean(“love”); //true
Ingeger wi2 = Integer.getInteger(“eigh”,999);
println(wi2.toString()); //若最上面那行有加-D的話為”8”,否則為”999”
|
methods
|
Integer
| static Integer getInteger(String key)
static Integer getInteger(String key, int val)
static Integer getInteger(String key, Ingeter val)
| Long
| static Long getLong(String key)
static Long getLong(String key, int val)
static Long getLong(String key, Long val)
| Boolean
| static boolean getBoolean(String key)
|
|
注意
| 參數”key”指的是propery name,”val”是defalut value,若system.propery內(java.version、java.home....)沒有定義所指定的key,則return ”val”。
|
新增系統參數
|
用System所定義的class method
| System.setProperty(“abc”,”33”);
| 啟動java interpreter時使用-D option
| java –Dabc=88 Test
|
|
取得系統參數
|
System所定義的class method
| n String val = System.getProperty(“abc”);
u 取key為”abc”的系統參數,若沒定義”abc”則傳回”null”。
n String val = System.getProperty(“abc”,”33”);
u 取key為”abc”的系統參數,若沒定義”abc”則傳回”33”。
| Integer、Long及Boolean所定義的class methods
| n Integer wi = getInteger(“abc”);
u 取key為”abc”的系統參數,若沒定義”abc”或numeric format不合,則傳回null。
n Ingeger wi = getInteger(“abc”,33);
u 取key為”abc”的系統參數,若沒定義”abc”則傳回”33”。
|
|
l The Math Class
注意
| n 是一個final class,故不可被繼承,且所有的mebers都是static members。
n 只有private constructor,故無法create一個Math object(也不須create Math object)
n 不會throws任何的exception。
n 對負數做開根號sqrt()所得結果為NaN。
n Long、Ingeger的最小絕對值是負的。
|
舉例
| n sin 45度角的值計算方式:(r即為最後的值)
u double d = Math.toRadians(45.5); double r = Math.sin(d);
|
簡單的記
|
(int、float、long、double)
| abs、max、min
| 奇怪的
| double random()、
long round(double a)、int round(float a)
| 只有double
| 剩下的其他method
|
|
constants
| n static final double E // e(2.718) ; static final double PI // π(3.141)
|
methods
|
三角函數
| double sin(double a) // sign of a
double cos(double a) // cosign of a
double tan(double a)// tangent of a
//”a”是angle(角度)單位為radians(弧度)
| 角度 / 弧度
| double toRadians(double deg) // 單位為degrees(度數)
double toDegrees(double rad) // 單位為radians(弧度)
| 次方
| double exp(double a) // ea
double pow(double a,double b) // ab
double sqrt(double a) // a開根號
| 亂數
| double random()//大於0.0但小於1.0的隨機數值
| 最大/最小 整數
| double ceil(double a) // >= ”a”的最小整數(無條件進入)
double floor(double a) //<= ”a”的最大整數(無條件捨去)
| 最接近”a”的整數
| double rint(double a)
int round(float a)
long round(double a)
| 絕對值
| int abs(int a)
long abs(long a)
float abs(float a)
double abs(double a)
| 較大值
| int max(int a, int b)
long max(long a, long b)
float max(float a, float b)
double max(double a, double b)
| 較小值
| int min(int a, int b)
long min(long a, long b)
float min(float a, float b)
double min(double a, double b)
|
|
l String and StringBuffer Classes
String
| n String object是不可變的,產生之後”值”就不可被改變,大部份的method都是return一個新的string object,所以String class內不會有setXxxx() method。
n 空值不能呼叫concat(),否則會runtime error。
產生字串
| u toString()
u static String valueOf(type a)//type為各個primite type
| 字串長度
| instance method — int length()
| 取出字串
| u String substring(ing begIndex)
//”abcdef”.substring(1)èbcdef
u String substring(int begIndex,int endIndex)
//”abcdef”.substring(1,3)èbcde
| 串接字串
| u String concat(String str)//附加在原string的後面
| 取出字元
| u char charAt(int index)
//char ch = “abcd”.charAt(2)è傳回’c’
u void getChars(int srcBegin,int srcEnd,char[] dst,int dstBeg)
//char[] ca=new char[5] ; abcdef.getChars(1,4,ca,1);
èca內容為{‘/u0000’,’b’,’c’,’d’,’/u0000’}
u char[] toCharArray() //把String轉換為char array
| 比較字串
| u boolean equals(Object anObject)
u boolean equalsIgnoreCase(String anotherString)
u boolean startsWith(String prefix)
u boolean startsWith(String prefix,int offset)
u boolean endsWith(String suffix)
u int compareToIgnoreCase(String str)
u int compareTo(String argStr)
//int i = str.compareTo(argStr)è0為str與argStr相同
| 尋找字串
| u int indexOf(int ch);int indexOf(int ch, int fromIndex)
u int lastIndexOf(int ch);int lastIndexOf(int ch, int fromIndex)
u int indexOf(String str);int indexOf(String str,int fromIndex)
u int lastIndexOf(String a);int lastIndexOf(String a,int frIndex)
// return -1代表沒找到
| 取代字元
| u String replace(char oldChar, char newChar)
// “abbbcdeb”.replace(‘b’,’k’)è”akkkcdek”
| Trim
| u String trim()
//移除string前後兩端的”white space”(如空白或’/t’...)
| intern
| u intern()
//將string加入string pool(若pool尚無相同content的string object)
|
|
StringBuffer
| n StringBuffer是可變的。
n StringBuffer沒有override equals()
續接字串
| u StringBuffer append(Object obj)
u StringBuffer append(String str)
u StringBuffer append(char[] str)
u StringBuffer append(char[] str, int offset, int len)
u StringBuffer append(boolean b);StringBuffer append(char c)
u StringBuffer append(int i);StringBuffer append(long l)
u StringBuffer append(float f);StringBuffer append(double d)
| 插入字串
| u StringBuffer insert(int index, char[] str, int offset, int len)
u StringBuffer insert(int offset, Object obj)
u StringBuffer insert(int offset, String str)
u StringBuffer insert(int offset, char[] str)
u StringBuffer insert(int offset, boolean b)
u StringBuffer insert(int offset, char c)
u StringBuffer insert(int offset, int i)
u StringBuffer insert(int offset, long l)
u StringBuffer insert(int offset, float f)
u StringBuffer insert(int offset, double d)
| 刪除字串
| u StringBuffer delete(int start, int end)
u StringBuffer deleteCharAt(int index)
| 取代字串
| u StringBuffer replace(int start, int end, String str)
| 顛倒字串
| u StringBuffer reverse()
|
|
来自:【 Garfield 的 SCJP 閱讀筆記 】