溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java 中String.equals與==有什么不同

發(fā)布時(shí)間:2020-12-02 16:22:55 來源:億速云 閱讀:161 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)java 中String.equals與==有什么不同,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

 java 中String.equals和==的比較

 初學(xué)java有段時(shí)間了,但是昨晚忽然就被"asd"==getpara("password")搞得不開心了:確實(shí)JAVA很多東西和以前接觸過的語言完全不一樣,比如最簡單的new String("asd") !=new String ("asd")。

1 一個(gè)最簡單的程序:

public class A {
  public static void main(String args[]) {
    String s1 = "Hello";
    String s2 = "Hello";
    System.out.println(s1 == s2);
  }
}

肯定很多人說,輸出true,確實(shí)是輸出true,s1和s2引用的是同一個(gè)東西嘛。但是這個(gè)程序如果換一種寫法,那么肯定就不太一樣了。

2 第一種變體:

public class A {
  public static void main(String args[]) {
    String s1 = "Hello";
    String s2 = new String("Hello");
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
  }
}

或者:

public class A {
  public static void main(String args[]) {
    String s1 = new String("Hello");
    String s2 = new String("Hello");
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
  }
}

那么結(jié)果就有點(diǎn)不太確定了,有些人很壞的想到了,單獨(dú)拿出來,肯定至少有一個(gè)不會(huì)相等。

這兩個(gè)程序的結(jié)果都是false true,原因很簡單,JAVA是一個(gè)基本完全面向?qū)ο蟮恼Z言,s1 == s2 判斷的是他的引用(相當(dāng)于內(nèi)存地址、指針),equals則是使用這個(gè)對(duì)象自身的方法去判斷值是否相等。

在以下表達(dá)式中:

String s1 = "Hello";
String s2 = new String("Hello");

JVM得到的是兩個(gè)不同的信息:

1.我要?jiǎng)?chuàng)建一個(gè)“Hello”字符串。
2.我要?jiǎng)?chuàng)建一個(gè)新的“Hello”字符串,不要跟原來的一樣的。

于是,s1 != s2.

3 第二種變體:

順序排列:

public class A {
  public static void main(String args[]) {
    String s1 = new String("Hello");
    String s2 = new String("Hello");
    String s3 = "Hello";
    s1=s1.intern();
    s2=s2.intern();
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
    System.out.println(s3 == s2);
    System.out.println(s3.equals(s2));
  }
}

打亂順序排列:

public class A {
  public static void main(String args[]) {
    String s1 = new String("Hello");
    String s3 = "Hello";
    s1=s1.intern();
    String s2 = new String("Hello");
    s2=s2.intern();
    //也可以String s2 = new String("Hello").intern();
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
    System.out.println(s3 == s2);
    System.out.println(s3.equals(s2));
  }
}

那么這次應(yīng)該很清楚了=

兩個(gè)object,但是中間加入了一些羞羞的東西,于是答案肯定是true true true true。

那么intern到底是什么。

4 intern作用:

當(dāng)調(diào)用 intern 方法時(shí),如果池已經(jīng)包含一個(gè)等于此 String 對(duì)象的字符串(該對(duì)象由 equals(Object) 方法確定),則返回池中的字符串。

否則,將此 String 對(duì)象添加到池中,并且返回此 String 對(duì)象的引用。

intern的適用情況:多個(gè)相同的大字符串同時(shí)出現(xiàn)的情況,例如可能出現(xiàn)多個(gè)相同消息的消息隊(duì)列的設(shè)計(jì)。

以上就是java 中String.equals與==有什么不同,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI