API補習 .java.lang包
1 原始類型 封裝類
char Char
int Integer
Integer num=new Integer(5);int num2=num.intValue; //1Integer num=Integer.valueof(2); //用於將相應的原始值轉換為其相應的封裝類;Charater isLetter() 是否是字母 isWhiteSpace() 是否為空白格或分行符號
2 StringBuffer類 (預設情況下,StringBuffer類保留的空間是16個字元),當使用+時自動產生StringBUffer.
用於表示可以修改的字串.
public class StringBuf { protected StringBuf(){} public static void main(String[] args){ StringBuffer buf=new StringBuffer("Java"); buf.append(" Guide Verl/"); //添加 buf.append(3);//添加 int index=5; buf.insert(index,"Student "); //插入 索引後的字元向後移 index=23; buf.setCharAt(index, '.'); //改變字元 int start=24; int end=25; buf.replace(start, end, "4"); //替換從哪到哪的字元 String s=buf.toString(); //StirngBuf和String間的轉換 System.out.println(s); }}
結果Java Student Guide Verl.4
第二個例子
public class Test { public static void stringReplace (String text) { text = text.replace('j' , 'i'); } public static void bufferReplace (StringBuffer text) { text = text.append("C"); } public static void main (String args[]) { String textString = new String ("java"); StringBuffer textBuffer = new StringBuffer ("java"); stringReplace (textString); bufferReplace (textBuffer); System.out.println (textString + textBuffer); } }
答案是 javajavaC
這是String參數傳遞,是不可變的(immutable).
而題目中第七行text = text.append (“C”),append方法會改變text中的值
而這個text與main中的textBuffer是指向同一個對象,所以對應的輸出是javac。
string的值永遠不會改變!
String a = "a";//假設a指向地址0x0001,
a = "b";//重新負值後a指向地址0x0002,但0x0001地址中儲存的"a"依舊存在,
但已經不再是a所指向的。
從表面上看String類型的對象改變了值,但事實是他不能改變值,只能改變指向
的地址;StringBuffer則不同,直接改變指向的地址中保留的值
例子三
StringBuffer s1 = new StringBuffer("a");StringBuffer s2 = new StringBuffer("a"); if( s1.equals(s2)) //為什麼是false { System.out.print("yes"); } String s11 = new String("a"); String s21 = new String("a"); if( s11.equals(s21))//為什麼是true { System.out.print("yes"); }
StringBuffer類中沒有重新定義equals這個方法,因此這個方法就來自Object類,
而Object類中的equals方法是用來比較地址的,所以等於false.
String類中重新定義了equals這個方法,而且比較的是值,而不是地址。所以會
是true。
3 Class類
執行個體.getClass() 由執行個體獲得對對象
getName() 獲得對象的名字
執行個體.getSuperclass() 由執行個體獲得父類