1. String 類的兩種實(shí)例化方式
- 直接賦值:String 變量 = "字符串" ,在構(gòu)造里面依然要接收一個本類對象
- 構(gòu)造方法:public String(String str);
public class StringDemo {
public static void main(String[] args) {
String str_1 = "Hello World"; //直接賦值
String str_2 = new String("Hello world"); //構(gòu)造方法賦
System.out.println(str_1 +" " +str_2 );
}
}
2. 字符串的比較
如果判斷兩個int型整數(shù)是否相等,可以直接用"==" 比較即可
在String上也可以使用"=="比較:
范例:在String對象上使用"=="比較
public class StringDemo {
public static void main(String[] args) {
String str_1 = "Hello World"; //直接賦值
String str_2 = new String("Hello world"); //構(gòu)造方法賦
String str_3 = str_2 ;
System.out.println(str_1 == str_2); //false
System.out.println(str_1 == str_3); //false
System.out.println(str_2 == str_3); //true
}
}
以上三個String對象內(nèi)容完全相等,但是結(jié)果卻不一樣;下面通過內(nèi)存圖分析:

String用"=="比較時(shí),內(nèi)存分析
通過以上的分析結(jié)果可以發(fā)現(xiàn),"=="比較的不是字符串對象的內(nèi)容,而是它們所在內(nèi)存地址的數(shù)值。
如果,要去比較字符串的內(nèi)容,可以使用String類里面的方法:
- 比較內(nèi)容:public boolean equals(String str);
范例:實(shí)現(xiàn)內(nèi)容的比較
public class StringDemo {
public static void main(String[] args) {
String str_1 = "Hello World"; //直接賦值
String str_2 = new String("Hello World"); //構(gòu)造方法賦
String str_3 = str_2 ;
System.out.println(str_1.equals(str_2)); //true
System.out.println(str_1.equals(str_3)); //true
System.out.println(str_2.equals(str_3)); //true
}
}
問題: "==" 與 "equals()"的區(qū)別
- "==":是Java提供的關(guān)系運(yùn)算符,主要的功能是進(jìn)行數(shù)值是否相等的判斷,如果在String對象上,則表示的是內(nèi)存的地址數(shù)值的比較;
- "equals()": 是由String提供的一個方法,此方法專門進(jìn)行字符串的內(nèi)容比較。
3. 字符串常量就是String的匿名對象
Java自己創(chuàng)造了字符串,但是這個字符串不屬于基本數(shù)據(jù)類型,它是將字符串作為了String類的匿名對象的形式存在
范例:觀察字符串是匿名對象的驗(yàn)證
public class StringDemo{
public static void main(String[] args){
String str = "Hello";
System.out.println("Hello".equals(str));
}
}
直接賦值實(shí)際上,就是將一個匿名對象設(shè)置了一個名字,但區(qū)別是,String類的匿名對象是由系統(tǒng)自動生成的,不再是由用戶自己直接創(chuàng)建。
另:為了避免空指針異常的出現(xiàn),可以將字符串寫在前面調(diào)用
public class StringDemo {
public static void main(String[] args) {
String input = null ; //假設(shè)這個內(nèi)容由用戶輸入
//如果用戶輸入的內(nèi)容是hello,認(rèn)為滿足一個條件
if(input.equals("hello")){
System.out.println("Hello world!!!");
}
}
}
******************************************************
Exception in thread "main" java.lang.NullPointerException
at String.StringDemo.main(StringDemo.java:7)
Process finished with exit code 1
由于用戶的輸入為空,input調(diào)用了equals()方法,所以出現(xiàn)了NullPointerException 異常;可以換個方式:
public class StringDemo {
public static void main(String[] args) {
String input = null ; //假設(shè)這個內(nèi)容由用戶輸入
//如果用戶輸入的內(nèi)容是hello,認(rèn)為滿足一個條件
if("hello".equals(input)){
System.out.println("Hello world!!!");
}
}
}
**************************************************
Process finished with exit code 0
如果將操作倒過來,永遠(yuǎn)不會出現(xiàn)空指針異常。在以后的開發(fā)中,如果要判斷輸入的內(nèi)容是否是某一字符串,將字符串寫在前面。
4. 總結(jié)
String類的特點(diǎn):
- String類對象的相等判斷使用equals()方法完成,"=="實(shí)現(xiàn)的是地址數(shù)值的比較;
- 字符串內(nèi)容一旦聲明則不可改變,String類對象內(nèi)容的改變是依靠引用關(guān)系的變更實(shí)現(xiàn)的;
- String類由兩種實(shí)例化方式,使用直接賦值可以不產(chǎn)生垃圾空間,并且可以自動入池,不要使用構(gòu)造方法。