String類 ,Arrays陣列工具類,Math類
String類
建立String字串的常見的3+1種方式
三種構造方法:
-
public String() 初始化一個新建立的 String 物件,使其表示一個空字元序列
-
public String(char[] array) : 根據字元陣列的內容,來建立對應的字串.
-
public String(byte[] array) :根據位元組陣列內容,來建立對應的字串.
一種直接建立:
-
public String = "ABC";
//演示程式碼
`
public class StringDemo01 { public static void main(String[] args) { //3+1種建立字串物件的方式 //1.使用空參構造方法來建立字串物件 String str1 = new String(); System.out.println("第一個字串:"+str1); //2.使用字元陣列建立字串物件 char[] ch={'A','B','C'}; String str2 = new String(ch); System.out.println("第二個字串:"+str2); //3.使用位元組陣列建立字串物件 byte[] byteArray={65,66,67}; String str3 = new String(byteArray); System.out.println("第三個字串:"+str3); //直接建立字串物件 String str4 = "ABC"; System.out.println("第四個字串:"+str4); } }
`
常用方法
判斷功能的方法
-
public boolean equals(Object anObject) :將此字串與指定的字串進行比較
`
public class StringDemo02 { public static void main(String[] args) { String str1 = "hello"; String str2 = "hello"; char[] ch = {'h','e','l','l','o'}; String str3 = new String(ch); System.out.println(str1 == str2);//true System.out.println(str2 == str3);//false System.out.println("---------------"); System.out.println(str1.equals(str2));//true System.out.println(str1.equals(str3));//true System.out.println(str2.equals(str3));//true String str4 = null; System.out.println(str1.equals(str4));//false,不會出現空指標 System.out.println(str4.equals(str1));//會出現空指標 } }
`
2. equalsIgnoreCase(String anotherString) : 將此 String 與另一個 String 比較,不考慮大小寫。
-
"==" 地址判斷,對於引用資料型別來說, == 比較的是記憶體地址,對於基本資料型別來說,比較的是兩個變數 的數值, 但是在字串直接賦值的時候,用 == 比較的是兩個字串的內容是否相等
獲取功能
. public int length() : 返回字串的長度
. public String concat(String str) : 將指定字串連線到此字串的結尾。
. public char charAt(int index) : 返回指定索引處的 char 值。
. public int indexOf(String str) : 返回指定子字串在此字串中第一次 出現處的索引值。
. public String substring(int beginIndex) : 返回一個新的字串,從beginIndex索引位置開始,它是此字串的一個子字串.
. String substring(int beginIndex, int endIndex) : 返回一個新字串,它是此字串的一個子字串,但是不包含endIndex位置上的字元。
public class StringDemo03 {
public static void main(String[] args) {
//初始化一個字串物件
String str = "ahsjsdhfdjhnsn324345";
//獲取str字串的長度
int length = str.length();
System.out.println("該字串的長度為:"+ length);
System.out.println("---------------------");
//拼接字串
String str2 ="Hello";
String str3 = "world";
String concat = str2.concat(str3);
System.out.println("拼接結果為:"+concat);
System.out.println("----------------");
//獲取指定索引處的單個字元
char c = "Hello".charAt(2);
System.out.println("索引值為2的字元值:"+c);
System.out.println("===================");
//查詢字串中指定子字串在字串中的
int i = str.indexOf("2");
System.out.println("數字2在str字串中第一次出現的索引值:"+i);
System.out.println("-------------------------");
//字串擷取
String str4 = "Helloworld";
int index = str4.indexOf("w");
String substring = str4.substring(index);
System.out.println("從w後的字串為:"+substring);
//獲取索引值[4,7]
//丟擲:
//IndexOutOfBoundsException - 如果 beginIndex 為負,
// 或 endIndex 大於此 String 物件的長度,或 beginIndex 大於 endIndex。
String substring1 = str4.substring(4, 8);
System.out.println("");
}
}
轉換功能
-
public char[] toCharArray() : 將此字串轉換成字元陣列 public byte[] getBytes() : 使用平臺預設的字符集將該字串編碼轉換成新的位元組陣列 public String replace(CharSequence oldString,CharSequence newString) :將所有指定的出現的老的字串,替換成新的字串,返回值是一個新的字串.CharSequence 表示的是接受的字串型別 備註:CharSequence 是一個介面,也是一種引用資料型別,意思是可以接受字串引數型別資料
static關鍵字
概述
static 關鍵字的使用,他可以修飾成員方法和成員變數,被修飾的成員方法和成員變數是屬於類的,不再單單屬於某個物件,既然是屬於類的,那就不需要通過建立物件來呼叫類中的成員資訊
定義和使用格式
類變數
黨成員變數被static修飾後,該變數就稱為類變數.該類的每個物件都共享同一個類變數值,任何物件都可以更改類變數的值.類變數也可以不通過建立類物件的方式進行操作,通過類也可以操作
定義格式
-
static 資料型別 變數名;
例如:static int roomID;
靜態方法
當成員方法一旦被static關鍵字修飾時,該方法稱為類方法.類方法一般是通過類名直接呼叫,而不需要單獨建立物件來呼叫.習慣性稱為靜態方法.
定義格式:
-
修飾符 static 返回值型別 方法名(引數列表){ //方法內容 }
例如:
public static String arrayToString(String[] arr){
//方法體內容如何轉換
//....
}
靜態方法呼叫的注意事項
-
靜態方法可以直接訪問類變數和靜態方法
-
靜態方法[不能直接訪問]成員變數和成員方法,反之,成員方法可以[直接訪問]類變數和靜態方法
-
靜態方法中不能使用this關鍵字
呼叫格式
靜態方法建議通過類名呼叫,但是也可以通過物件名呼叫,會出現警告資訊,不建議使用物件資訊來呼叫
格式:
-
//訪問類變數 類名.變數名; //訪問靜態方法 類名.方法名;
Static修飾的內容
-
是隨著類的載入而載入,而且只會載入一次 儲存於一塊固定的記憶體區域(方法區的靜態域),所以,可以被類直接呼叫 他優先於物件存在,所以可以被類的所有物件所共享
靜態程式碼塊
靜態程式碼塊:被static修飾的,定義在成員位置的程式碼塊
-
位置:類中,方法外
-
執行:隨著類的載入而載入,只會執行一次,優先於main方法和構造方法執行
格式:
-
public class ClassName{ //靜態程式碼塊,存在於成員位置 static{ //執行語句,多條執行語句 //... } }
備註:static可以修飾成員變數,成員方法,程式碼塊.在使用過程中,如果你不想在不建立物件的情況下,去呼叫成員變數和成員方法,可以定義成靜態的,對於靜態程式碼塊來說,通常作用是對靜態變數初始化