String類的基本概念

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)造方法。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評論 18 399
  • 前言 最先接觸編程的知識是在大學(xué)里面,大學(xué)里面學(xué)了一些基礎(chǔ)的知識,c語言,java語言,單片機(jī)的匯編語言等;大學(xué)畢...
    oceanfive閱讀 3,395評論 0 7
  • 一、Java 簡介 Java是由Sun Microsystems公司于1995年5月推出的Java面向?qū)ο蟪绦蛟O(shè)計(jì)...
    子非魚_t_閱讀 4,564評論 1 44
  • 在這篇文章中,我關(guān)注的是 Objective-C 中的一個陌生的概念—— meta-class。在 Objecti...
    GentlePrince閱讀 833評論 0 1
  • 杭州銀行成立于1996年9月,總部位于杭州。目前,全行擁有189家分支機(jī)構(gòu),網(wǎng)點(diǎn)覆蓋長三角、珠三角、環(huán)渤海灣等...
    HZbank閱讀 2,340評論 0 0

友情鏈接更多精彩內(nèi)容