Java中的String類解析

1.相等性的比較(==)
(1)對於原生數據類型來說,比較的是左右兩邊的值是否相等
(2)對於引用類型來說,比較左右兩邊的引用是否指向同一個對象,或者說左右兩邊的引用地址是否相同。

2.Object 類的tostring方法返回的是一個哈希code值,而string類重寫了tostring方法,默認調用tostring方法。
API (Application Programming Interface),應用編程接口。
當打印引用時,實際上會打印出引用所指對象的toString()方法的返回值,因爲每個類都直接或間接地繼承自Object,而Object類中定義了toString(),因此每個類都有toString()這個方法。

3.equals方法
對於object類的equals方法來說,是用來判斷兩個對象是不是同一個對象。
對於繼承了object類的其他類來說,如果重寫了equals方法,纔是判斷內容是否一致,,如果沒有重寫equals方法,是判斷地址是否一致。
對於String類的equals()方法來說,它是判斷當前字符串與傳進來的字符串的內容是否一致。

4.字符串是一個常量,創建之後值是不能被改變的。

5.String是常量,其對象一旦創建完畢就無法改變。當使用+拼接字符串時,會生成新的String對象,而不是向原有的String對象追加內容。

6、 String Pool(字符串池)

7、 String s = “aaa”;(採用字面值方式賦值)
1) 查找String Pool中是否存在“aaa”這個對象,如果不存在,則在String Pool中創建一個“aaa”對象,然後將String Pool中的這個“aaa”對象的地址返回來,賦給引用變量s,這樣s會指向String Pool中的這個“aaa”字符串對象
2) 如果存在,則不創建任何對象,直接將String Pool中的這個“aaa”對象地址返回來,賦給s引用。

8、 String s = new String(“aaa”);
1) 首先在String Pool中查找有沒有“aaa”這個字符串對象,如果有,則不在String Pool中再去創建“aaa”這個對象了,直接在堆中(heap)中創建一個“aaa”字符串對象,然後將堆中的這個“aaa”對象的地址返回來,賦給s引用,導致s指向了堆中創建的這個“aaa”字符串對象。
2) 如果沒有,則首先在String Pool中創建一個“aaa“對象,然後再在堆中(heap)創建一個”aaa“對象,然後將堆中的這個”aaa“對象的地址返回來,賦給s引用,導致s指向了堆中所創建的這個”aaa“對象。

9.new出來的對象都是在堆裏面

10.intern()方法

public class Test
{
    public static void main(String[] args)
    {
        Object object = new Object();
        Object object2 = new Object();

        //object類中的equals方法是使用==判斷的
        System.out.println(object == object2);//false

        System.out.println("----------------");
        String str = new String("aaa");
        String str2 = new String("aaa");

        System.out.println(str == str2);//false兩個對象

        System.out.println("----------------");

        //StringPool字符串池在棧中,當字符串被賦予字面值的時候,首先檢查字符串池裏面有沒有該對象"bbb"
        //如果沒有,就將該字符串放入字符串池裏面,字符串變量便指向這個對象
        //如果有,就不會在字符串池裏面創建新的對象,而是在已有的字符串池裏面的對象直接返回來賦給字符串變量str4
        //所以str4並沒有創建對象
        //new出來的對象都是在堆裏面
        String str3 = "bbb";//創建一個對象
        String str4 = "bbb";//並沒有創建對象

        System.out.println(str3 == str4);//true 指向同一個對象

        System.out.println("----------------");

        String str5 = new String("ccc");
        String str6 = "ccc";

        System.out.println(str5 == str6);//false

        System.out.println("----------------");

        //字符串的拼接並不是將字符串拼接到後面。字符串是常量,創建後就不能改變了,
        //加法操作實際上是生成了一個新的對象,而不是往原有的對象追加內容。
        String s = "hello";//
        String s1 = "hel";
        String s2 = "lo";

        System.out.println(s == s1 + s2);//false

        System.out.println("----------------");
        System.out.println(s == "hel" + "lo");//true
    }
}
package testPackage;
class Test {
        public static void main(String[] args) {
                String hello = "Hello", lo = "lo";
                System.out.print((hello == "Hello") + " ");
                System.out.print((Other.hello == hello) + " ");
                System.out.print((other.Other.hello == hello) + " ");
                System.out.print((hello == ("Hel"+"lo")) + " ");
                System.out.print((hello == ("Hel"+lo)) + " ");
                System.out.println(hello == ("Hel"+lo).intern());
        }
}
class Other { static String hello = "Hello"; }
and the compilation unit:
package other;
public class Other { static String hello = "Hello"; }
produces the output:

true true true true false true

This example illustrates six points:

1.Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
2.Literal strings within different classes in the same package represent references to the same String object.
3.Literal strings within different classes in different packages likewise represent references to the same String object.
4.Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
5.Strings computed by concatenation at run time are newly created and therefore distinct.

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