Object類、String類、Arrays類、StringBuffer類、StringBuilder的總結1
阿新 • • 發佈:2018-11-03
Object類
是所有類的頂層父類。
圖中漏了一個非常重要的方法,toString()方法,具體用法會在後面程式碼中詳細解釋。
所有類都預設繼承了Object類,所有具有Object類的這些方法,光說不練假把式。上栗子:
我建立了一個測試類Test,但是我並沒有讓他繼承Object類,但是預設繼承,我就可以呼叫Object類的非私有修飾的所有方法。
equals方法
返回值:布林型別
由於沒有重寫Object的方法,所有預設比較的是兩個Student類物件的地址值,故輸出false
getClass方法
返回值:返回執行時類的方法,反射中會用到該方法
hashCode方法
返回值:一個int型別的數
該方法內部通過雜湊演算法計算返回一個呼叫者對應的雜湊碼值。
toString方法
返回值:String型別
沒有重寫該方法的時候會列印對應地址值與十六進位制拼成的字串,重寫以後以重寫的方法進行對應輸出。請看栗子
//一個Student實體類,未重寫toString方法 public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Test {
public static void main(String[] args) {
Student student1 = new Student();
System.out.println(student1.toString());
}
}
執行結果:
//重寫父類toString方法 public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }
public class Test {
public static void main(String[] args) {
Student student1 = new Student("張三",12);
System.out.println(student1.toString());
}
}
執行結果 :
String類
charAt方法(public char charAt(int index))
引數:字串對應索引值(int型別)
返回值 :char型別
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.charAt(5));
}
}
執行結果: f
注意:跟陣列一樣下標從0開始,越界會拋異常 StringIndexOutOfBoundsException
length方法(public int length())
返回值 返回int型別的長度
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.length());
}
}
執行結果 : 7
concat方法(public String concat(String str))
引數:String型別
返回值:String型別
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.concat("sssss"));
}
}
執行結果: abcdefgsssss
isEmpty方法 (public boolean isEmpty())
返回值:布林型別
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.isEmpty());//字串不為空就返回false,反之返回true
}
}
執行結果:false
toUpperCase方法( public String toUpperCase())
返回值:String型別
public class Test {
public static void main(String[] args) {
String s ="abcdefg";
System.out.println(s.toUpperCase());//將字串中小寫字母全部轉換為大寫字母
}
}
執行結果:ABCDEFG
toLowerCase方法(public String toLowerCase())
返回值:String型別
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
System.out.println(s.toLowerCase());//將字串中大寫字母全部轉換為小寫字母
}
}
執行結果:abcdefg
toCharArray方法(public char[] toCharArray())
返回值:字元陣列char[]
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
char[] chars = s.toCharArray();//將字串轉為字元陣列
}
}
getBytes方法(public byte[] getBytes())
返回值:位元組陣列
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
byte[] bytes = s.getBytes();//將字串轉為位元組陣列
}
}
replace方法(public String replace(char oldChar,char newChar))
引數:第一個引數是需要被替換的字串,第二個引數是需要替換成的字串
返回值:String型別
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
System.out.println(s.replace("A","*"));
}
}
執行結果:*BCDEFG
substring方法
public String substring(int beginIndex)
引數:擷取的索引處的索引值 int型別
返回值:String型別
public class Test {
public static void main(String[] args) {
String s ="ABCDEFG";
System.out.println(s.substring(2));
}
}