Java基礎突擊第二天0005

類屬於引用數據類型,一段堆空間可以同時被多個棧內存指向。

class Person{

    String name;   int age;

}

Person per1 = new Person();

Person per2 = per1;

per1.name = "FangXy";   per1.age = 11;

此時per2.name == "FangXy";  per2.age ==11;

per1 = null;  per2 = null;

此時堆內存儲的name和age變成了垃圾。等待垃圾收集機制來釋放空間。

Java本身提供垃圾收集機制,會不定期的釋放不用的內存空間,只要對象不再使用,就會等待GC釋放空間。


String可以通過直接賦值和實例化賦值。

String str1 = "Hello World!";

String str2 = new String("Hello World!");

str1 == str2 -> false,==指代的是字符串地址。只有在兩個變量指向同一個字符串的時候==才爲true;

str1.equals(str2) -> true;

一個字符串就是一個String類的匿名對象(已經開闢了堆內存空間並可以使用的對象)。

直接賦值法:若有一個字符串已經被一個字符串對象引用,以後再有相同的字符串聲明時,就不會重新開闢空間。

String str1="Hello",str2 ="Hello",str3 = "Hello";

-> str1 == str2 ==str3

用new String,每一個new都會重新分配一個新的空間。

String str4 =new String("Hello");

因爲每個字符串就是一個String類的匿名對象,“Hello”佔用了一段內存空間;

且new又開闢了一個“Hello”內存空間。所以上述聲明語句相當於開闢了兩段內存空間。


字符串的內容一旦聲明,不可改變。

String str = "hello"; //在堆內存開闢空間分給hello,將內存地址賦值給str

str = str + "world";  //在堆內存中開闢空間分給world,在堆內存中開闢空間分給“hello world”

//將“hello world”的堆內存地址賦值給str,此時堆內存“hello”“world”變成了垃圾。等待回收。

所以如下代碼的性能時很低的。

String str1 = "FangXy";

for(int i=0;i<100;i++){

    str1 +=1;  //每次結合造成兩次垃圾,循環100次性能極其低下。需要此種情況時,可考慮StringBuffer類。

}

String常用方法:

構造方法: public String(char[] value)

                  public String(char[] value,int offset,int count) //將指定範圍的字符數組變爲字符串。

                  public String(byte[] value)

                  public String(byte[] value,int offset,int length) //將指定範圍的byte數組變爲字符串。

普通方法: public char[]  toCharArray()      //String轉換字符數組

                  public char charAt(int index)    //從特定位置取出字符

                  public byte[] getBytes()             //String轉換爲byte數組

                  public int length()

                  public int indexOf(String str)

                  public int indexOf(String str , int fromIndex)  //從指定位置開始查找指定字符串位置。

                  public String trim()                                          //清除左右兩端的空格

                  public String substring(int beginIndex)    //從指定位置開始截取字符串到末尾

                  public String substring(int begin, int end)//指定的首尾位置截取字符串

                  public String[] split(String regex)               //按照指定字符串對字符串進行拆分,regex是正則表達式的意思

                  public String toUpperCase()

                  public String toLowerCase()

                  public boolean startsWith(String prefix)      //判斷是否以指定字符串開頭

                  public boolean endsWith(String suffix)        //判斷是否以指定字符串結尾

                  public boolean equals(String str)

                  public boolean equalsIgnoreCase(String str)  //不區分大小寫比較兩個字符串是否相等。

                  public String replaceAll(String regex, String replacement)

具體應用如下代碼

	        public static void printChar(char array[]){
			for(int i=0;i<array.length;i++){
				System.out.print(array[i]+" ");
			}
			System.out.println();
		}//printChar
		public static void printByte(byte array[]){
			for(int i=0;i<array.length;i++){
				System.out.print(array[i]+" ");
			}
			System.out.println();
		}//printByte
		public static void main(String[] args){
			String str1="Hello",str2 ="Hello";
			String str3 = new String("Hello");
			System.out.println(str1 == str2);
			System.out.println(str1 == str3);

			char c[] = str1.toCharArray();
			printChar(c);
			char cUpper[] = str1.toUpperCase().toCharArray();
			char cLower[] = str1.toLowerCase().toCharArray();
			printChar(cUpper);
			printChar(cLower);

			byte b[] = str1.getBytes();
			printByte(b);

			System.out.println(new String(b));
			System.out.println(new String(b,0,2));
			System.out.println(new String(c));
			System.out.println(new String(c,1,2));

			System.out.println("The length of str1 is "+str1.length());

			int indexL1 = str1.indexOf('l');
			System.out.println("The position of l in hello is "+(indexL1+1));
			int indexL2 = str1.indexOf("l",indexL1+1);
			System.out.println("The position of l in hello is "+(indexL2+1));
			System.out.println("The position of x in hello is "+(str1.indexOf("x")+1));

			String strTrim = "  Hello!  ";
			System.out.println("Original:  Hello!  After trimming:"+strTrim.trim());

			String subToTear = str1.substring(2);
			System.out.println(subToTear);
			String subBeginEnd = str1.substring(2,4); //the end character is not included
			System.out.println(subBeginEnd);

			String strSplit = "Hello WorLd";   //the last L is capital
			String strArray[] = strSplit.split("l");  // "l" is correct,not 'l'
			for(int i = 0;i<strArray.length;i++){
				System.out.print(strArray[i]+"/");
			}
			System.out.println();
			System.out.println("The length of String Array is "+strArray.length);
			//The splited array is "He"  ""  "o WorLd"

			System.out.println(str1.equals("HELLO"));
			System.out.println(str1.equalsIgnoreCase("HELLO"));

			System.out.println(str1.startsWith("h"));  // " " is correct,not ''
			System.out.println(str1.startsWith("H"));
			System.out.println(str1.endsWith("o"));
			System.out.println(str1.endsWith("O"));

		  System.out.println("After replace:"+str1.replaceAll("l","x"));
		}//main

輸出:

true
false
H e l l o
H E L L O
h e l l o
72 101 108 108 111
Hello
He
Hello
el
The length of str1 is 5
The position of l in hello is 3
The position of l in hello is 4
The position of x in hello is 0
Original:  Hello!  After trimming:Hello!
llo
ll
He//o WorLd/
The length of String Array is 3
false
true
false
true
true
false
After replace:Hexxo




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章