l Wrapper Classes(包裝類別)
l 特別注意常出現狀況
n Wrapped values不能被更改。
n 不能直接把Boolean拿來evaluate true/false值,否則會compiler error。
n 不同class的wrapper class,即使值相同,用equals()去比也是false。
n primitve xxxValue() //不接受任何引數,回傳primitive
n primitive parseXXX(String) //只接受String引數,回傳primitive
n Wrapper valueOf(String) //只接受String引數,回傳wrapper object
l primitive data types / wrapper classes
primitive types |
Wrapper classes |
boolean |
Boolean |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
char |
Character |
float |
Float |
double |
Double |
l 用Primitives產生String / 從String中剖析出Primitives
classes |
primitive value轉成 string object |
string object剖析成 primitive values |
意義 |
直接將primitive value轉成string object |
直接將string object剖析成primitive values |
注意事項 |
n 為static method
n radix可是任意基底 |
n 為static method
n radix可是任意基底
n 會throws NumberFormatException
n return type是primitive type |
舉例 |
Integer.toBinaryString(15) //1111
Integer.toOctalString(15) //17
Integer.toHexString(15) //f
Integer.toString(15,3) //120 |
Integer.parseInt(“15”) //15
Integer.parseInt(“15”,16) //21
Integer.parseInt(“f”,16) //15
Integer.parseInt(“1010”,2) //10
Integer.parseInt(“0xf”,16) //丟出NumberFormatException |
Byte |
String toString(type s)
ex : String toString(byte s)
//type為對應之primite type(byte、short、boolean、char、float、double) |
long parseByte(String s)
long parseByte(String s,int radix) |
Short |
long parseShort(String s)
long parseShort(String s,int radix) |
Float |
float parseFloat(String s) |
Double |
double parseDouble(String s) |
Boolean |
|
Character |
|
Integer |
String toBinaryString(type i)
String toHexString(type i)
String toOctalString(type i)
String toString (type i)
String toString(type i,int radix)
// type為對應之primite type(int、long) |
int parseInt(String s)
int parseInt(String s,int radix) |
Long |
long parseLong(String s)
long parseLong(String s,int radix) |
l 由Primitive產生Wrapped objects / 取出Wrapped Objects內的Wrapped Value
classes |
由Primitive產生
Wrapped objects |
取出Wrapped Objects內的
Wrapped Value |
意義 |
將primitive value包裝成wrapped object |
取出wrapped objects內的wrapped value |
注意事項 |
|
n 為instance method |
舉例 |
Integer wi = new Integer(15);
Boolean wb = new Boolean(true); |
Integer wi = new Integer(15);
println(wi.doubleValue()); //15.0 |
Boolean |
Type(type val)
ex : Boolean(boolean val)
// Type為對應之Wrapped Object
(Boolean、Character、...)
// type為對應之primite type
(boolean、char、...) |
boolean booleanValue() |
Character |
char charValue() |
Short |
byte byteValue()
short shortValue()
ing intValue()
long longValue()
float floatValue()
double doubleValue() |
Integer |
Long |
Byte |
Float |
Double |
l 從String產生Wrapped Object
classes |
method |
意義 |
將對應的primitive value包裝成wrapped object。 |
注意事項 |
n 為constructors或static methods。
n static methods的return type是wrapper class而非primitive type。
n 都會throws NumberFormatException。
n 若string的開頭是”0X”、”0x”、”#”,則以16為基底來parse(開頭0以8為基底),否則以10為基底來parse。
n radix可是任意基底。
valueOf(String s) |
以10為基底來parse string ”s”。 |
decode(String s) |
先看”s”字串的開頭為何再決定以16、10或8為基底來parse。 |
|
舉例 |
Integer.decode(“15”).byteValue() //15
Integer.decode(“030”).shortValue() //24
Integer.decode(“0x0f”).intValue() //15
Integer.valueOf(“100”).byteValue() //100
Integer.valueOf(“2w”).byteValue() //throws NumberFormatException
Integer.valueOf(“f”,16).byteValue() //15
Integer.valueOf(“20”,3).byteValue() //6 |
Character |
|
Boolean |
Type(String s) // constructor
static Type valueOf(String)
// Type為對應之Wrapped Object(Boolean、Float、Double) |
Float |
Double |
Short |
Type(String s) // constructor
static Type decode(String s)
static Type valueOf(String s)
static Type valueOf(String s,int radix)
// Type為對應之Wrapped Object(Short、Integer、Long、Byte) |
Integer |
Long |
Byte |
l Character內常用的Class Methods
用途 |
改變或判斷char value |
舉例 |
Character.isLetter(‘a’); // true
Character.isDigit(‘5’); // true
Character.toLowerCase(‘Q’); // q |
methods |
static boolean isDigit(char ch)
static boolean isLetter(char ch)
static boolean isLetterOrDigit(char ch)
static boolean isLowerCase(char ch)
static boolean isUpperCase(char ch)
static boolean isWhiteSpace(char ch)
static char toLowerCase(char ch)
static char toUpperCase(char ch) | 来自于:【 Garfield 的 SCJP 閱讀筆記 】
|