一,“==”與equals()
運行以下代碼,如何解釋其輸出結(jié)果?
1
2
3
4
5
6
7
8
9
10
11
|
public class StringPool { public static void main(String args[]) { String s0= string">"Hello" ; String s1= "Hello" ; String s2= "He" + "llo" ; System.out.println(s0==s1); //true System.out.println(s0==s2); //true System.out.println( new String( "Hello" )== new String( "Hello" )); //false } } |
首先s0==s1
在Java執(zhí)行時會維護一個String堆,對于一些可以共享的字符串對象,會先在堆中查找是否存在相同的String內(nèi)容(字符相同),如果有就直接返回,不創(chuàng)建新對象。
s0中的值是引用的s1的值,自己并沒有創(chuàng)建對象,所以比較后的結(jié)果是true。
同理,s2中的值也是引用S1的值,所以比較的結(jié)果也是true
new String("Hello")==new String("Hello")
同時在堆中new了兩個對象,這兩個對象的內(nèi)容都是Hello,
但就好比a籃子和b籃子都裝了一個蘋果,a籃子裝了蘋果后和b籃子裝了蘋果后能判相等嗎?
當然不行,假設(shè)蘋果都是一樣的,那蘋果當然能和蘋果相等,但是籃子卻是不一樣的
在Java中,內(nèi)容相同的字串常量(“Hello”)只保存一份以節(jié)約內(nèi)存,所以s0,s1,s2實際上引用的是同一個對象。
編譯器在編譯s2一句時,會去掉“+”號,直接把兩個字串連接起來得一個字串(“Hello”)。這種優(yōu)化工作由Java編譯器自動完成。
當直接使用new關(guān)鍵字創(chuàng)建字符串對象時,雖然值一致(都是“Hello”),但仍然是兩個獨立的對象。
Java中“==”的使用
1基本數(shù)據(jù)類型:比較的是內(nèi)容;
2引用數(shù)據(jù)類型:比較的是對象地址;
再看以下代碼
1
2
3
4
5
6
7
8
9
10
|
public static void main(String args[]) { String s1= "a" ; String s2=s1; System.out.println(s1==s2); //true s1+= "b" ; System.out.println(s1==s2); //false System.out.println(s1== "ab" ); //false System.out.println(s1.equals( "ab" )); //true } |
分析:
給字串變量賦值意味著:兩個變量(s1,s2)現(xiàn)在引用同一個字符串對象“a”!
String對象的內(nèi)容是只讀的,使用“+”修改s1變量的值,實際上是得到了一個新的字符串對象,其內(nèi)容為“ab”,它與原先s1
所引用的對象”a”無關(guān),所以,s1==s2返回false;
代碼中的“ab”字符串是一個常量,它所引用的字符串與s1所引用的“ab”對象無關(guān)。
String.equals()方法可以比較兩個字符串的內(nèi)容。
二,String,equals()
方法
java中的String.equals()方法的實現(xiàn)代碼:
equals()法是根類Object中的方法。源代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public boolean equals(Object obj) { return ( this == obj); } //可見默認的equals方法,直接調(diào)用==,比較對象地址。 // //不同的子類,可以重寫此方法,進行兩個對象的equals的判斷。 //String類源碼中重寫的equals()方法的實現(xiàn)代碼如下: public boolean equals(Object anObject) { if ( this ==anObject) return true ; if (anObject instanceof String) { String anotherString=(String)anObject; int n=value.length; if (n==anotherString.value.length) //若兩個字符串長度一樣,則一個個進行字符比較 { char v1[]=value; //字符串轉(zhuǎn)化成的對應(yīng)數(shù)組 char v2[]=anotherString.value; //字符串轉(zhuǎn)化成的對應(yīng)數(shù)組 int i= 0 ; while (n--!= 0 ) { if (v1[i]!=v2[i]) return false ; //若比較過程中出現(xiàn)不等,則倆字符串不等,返回false i++; } return true ; //直至比較完兩個字符串長度,跳出while循環(huán) // 此時說明倆字符串相等,返回true } } return false ; //兩個字符串長度不一樣,倆字符串不等, //不必一個個比較內(nèi)容,直接返回false } |
注:instanceof是Java、php的一個二元操作符(運算符),和==,>,<是同一類東西。由于它是由字母組成的,所以也是Java的保留關(guān)鍵字。它的作用是判斷其左邊對象是否為其右邊類的實例,返回boolean類型的數(shù)據(jù)。可以用來判斷繼承中的子類的實例是否為父類的實現(xiàn)。
從上面的代碼可以得知:
(1) String類中的equals首先比較地址,如果是同一個對象的引用,可知對象相等,返回true。
(2)如果不是同有一個對象,equals方法則繼續(xù)挨個比較兩個字符串對象內(nèi)的字符,只有完全相等才返回true,否則返回false。
三,整理String類的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()toCharArray()
使用說明
Length():獲取字串長度
charAt():獲取指定位置的字符
getChars():獲取從指定位置起的子串復(fù)制到字符數(shù)組中
replace():子串替換
toUpperCase()、 toLowerCase():大小寫轉(zhuǎn)換
trim():去除頭尾空格
toCharArray():將字符串對象轉(zhuǎn)換為字符數(shù)組
四,String類的方法可以連續(xù)調(diào)用:
1
2
|
String str= "abc" ; String result=str.trim().toUpperCase().concat( "defg" ); |
請閱讀JDK中String類上述方法的源碼,模仿其編程方式,編寫一個MyCounter類,它的方法也支持上述的“級聯(lián)”調(diào)用特性,其調(diào)用示例為:
MyCounter counter1=new MyCounter(1);
MyCounter counter2=counter1.increase(100).decrease(2).increase(3);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class MyCounter { int i; MyCounter( int n){ i=n; } public MyCounter increase( int n) { this .i= this .i+n; return this ; } public MyCounter decrease( int n) { this .i= this .i-n; return this ; } public static void main(String[] args) { MyCounter counter1= new MyCounter( 1 ); MyCounter counter2=counter1.increase( 100 ).decrease( 2 ).increase( 3 ); System.out.println( "counter2.i=" +counter2.i); } } |
總結(jié)
以上就是本文關(guān)于淺談Java編程中string的理解與運用的全部內(nèi)容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://www.cnblogs.com/sdysyhj/p/7744407.html