關於字元串與包裝類的一些常識

来源:https://www.cnblogs.com/hapjin/archive/2018/12/04/10067875.html
-Advertisement-
Play Games

String str1 和 str2 所指向的對象在字元串常量池中,是同一個對象。 All literal strings and string valued constant expressions are interned,When the intern method is invoked, i ...


String

        //string intern pool
        String str1 = "a";
        String str2 = "a";

        System.out.println(str1.equals(str2));//true
        System.out.println(str1==str2);//true
        System.out.println(str1.hashCode());//97
        System.out.println(str2.hashCode());//97
  • str1 和 str2 所指向的對象在字元串常量池中,是同一個對象。

    All literal strings and string-valued constant expressions are interned,When the intern method is invoked, if the pool already contains a string equal to this {@code String} object as determined by the {@link #equals(Object)} method, then the string from the pool is returned.

String.intern()方法的註釋:

    /**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>
     * It follows that for any two strings {@code s} and {@code t},
     * {@code s.intern() == t.intern()} is {@code true}
     * if and only if {@code s.equals(t)} is {@code true}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     * @jls 3.10.5 String Literals
     */
    public native String intern();
        String str3 = "a";//string intern pool,constant pool
        String str4 = new String("a");//heap object
        System.out.println(str3.equals(str4));//true
        System.out.println(str3==str4);//false
  • str3所指向的對象在常量池中,str4所指向的對象分配在堆上,== 基於對象的地址來判斷,因此返回false
        String str5 = "a";
        String str6 = String.valueOf("a");//string intern pool, constant pool
        System.out.println(str5.equals(str6));//true
        System.out.println(str5 == str6);//true
  • 看String.valueOf的源碼如下:

        public static String valueOf(Object obj) {
            return (obj == null) ? "null" : obj.toString();
        }
  • 參數 obj 是 "a",也即是一個字元串對象,再看String.toString()方法源碼如下:返回的是this 對象本身。因此,System.out.println(str5 == str6);輸出true

        /**
         * This object (which is already a string!) is itself returned.
         *
         * @return  the string itself.
         */
        public String toString() {
            return this;
        }

Integer

        Integer integer1 = new Integer(1);// heap obj
        Integer integer2 = new Integer(1);//another heap obj
        System.out.println(integer1.equals(integer2));//true
        System.out.println(integer1 == integer2);//false
  • 採用關鍵字 new 創建對象,integer1 和 integer2 是堆上兩個不同的對象,因此System.out.println(integer1 == integer2)輸出false

  • JDK9已經不推薦使用 new 來創建 Integer對象,因為new沒有使用緩存,有性能問題。

        /**
         * Constructs a newly allocated {@code Integer} object that
         * represents the specified {@code int} value.
         *
         * @param   value   the value to be represented by the
         *                  {@code Integer} object.
         *
         * @deprecated
         * It is rarely appropriate to use this constructor. The static factory
         * {@link #valueOf(int)} is generally a better choice, as it is
         * likely to yield significantly better space and time performance.
         */
        @Deprecated(since="9")
        public Integer(int value) {
            this.value = value;
        }

        Integer integer3 = 1;
        Integer integer4 = 1;
        System.out.println(integer3.equals(integer4));//true
        System.out.println(integer3==integer4);//true
  • 包裝類Integer對範圍-128~127之間的數據進行了緩存。直接賦值方式使得:integer3 和 integer4 指向的是同一個對象。

     /**
         * Cache to support the object identity semantics of autoboxing for values between
         * -128 and 127 (inclusive) as required by JLS.
         *
         * The cache is initialized on first usage.  The size of the cache
         * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
         * During VM initialization, java.lang.Integer.IntegerCache.high property
         * may be set and saved in the private system properties in the
         * jdk.internal.misc.VM class.
         */
    
        private static class IntegerCache {
            static final int low = -128;
            static final int high;
            static final Integer cache[];

        Integer integer5 = 128;
        Integer integer6 = 128;
        System.out.println(integer5.equals(integer6));//true
        System.out.println(integer5 == integer6);//false
  • 128 超過了預設的緩存範圍,因此System.out.println(integer5 == integer6)輸出false
        Integer integer7 = 127;
        Integer integer8 = Integer.valueOf(127);
        System.out.println(integer7.equals(integer8));//true
        System.out.println(integer7 == integer8);//true
  • 127在緩存範圍之內,Integer.valueOf()方法返回的是緩存的對象,因此 integer7 和 integer8 指向的是同一個對象,故System.out.println(integer7 == integer8)輸出true

  • Integer.valueOf源碼如下:

        /**
         * Returns an {@code Integer} instance representing the specified
         * {@code int} value.  If a new {@code Integer} instance is not
         * required, this method should generally be used in preference to
         * the constructor {@link #Integer(int)}, as this method is likely
         * to yield significantly better space and time performance by
         * caching frequently requested values.
         *
         * This method will always cache values in the range -128 to 127,
         * inclusive, and may cache other values outside of this range.
         *
         * @param  i an {@code int} value.
         * @return an {@code Integer} instance representing {@code i}.
         * @since  1.5
         */
        @HotSpotIntrinsicCandidate
        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }

        Integer integer9 = 128;
        Integer integer10 = Integer.valueOf(128);
        System.out.println(integer9.equals(integer10));//true
        System.out.println(integer9 == integer10);//false
  • 128超過了預設緩存範圍,因此System.out.println(integer9 == integer10)輸出false

寫了這麼多並不是說要記住每種情況,而是要瞭解以下幾個點:

  1. String 字元串常量池,字面量都存儲在常量池中。
  2. == 比較 與equals比較區別。
  3. equals比較與hashCode方法的聯繫。如果兩個對象equals返回true,那麼這兩個對象的hashCode肯定是相同的;如果兩個對象的equals返回false,兩個對象的hashCode可以是相同的,hashCode相同意味著發生了衝突,就是常討論的HashMap 的Key 衝突的情形。
  4. 基本數據類型有對應的包裝類,包裝類有數據緩存的功能,用對了地方能提升性能。
  5. 明白 = 賦值創建對象 和 new 創建對象的區別,new一個對象時,對象會分配在堆中。
  6. 瞭解JDK基礎類的常用方法的底層實現源碼

完整代碼:

public class StringIntegerCompare {

    public static void main(String[] args) {
        //string intern pool
        String str1 = "a";
        String str2 = "a";

        System.out.println(str1.equals(str2));//true
        System.out.println(str1==str2);//true
        System.out.println(str1.hashCode());//97
        System.out.println(str2.hashCode());//97

        String str3 = "a";//string intern pool,constant pool
        String str4 = new String("a");//heap object
        System.out.println(str3.equals(str4));//true
        System.out.println(str3==str4);//false
        System.out.println(str3.hashCode());//true
        System.out.println(str4.hashCode());//true

        String str5 = "a";
        String str6 = String.valueOf("a");//string intern pool, constant pool
        System.out.println(str5.equals(str6));//true
        System.out.println(str5 == str6);//true
        System.out.println(str5.hashCode());//97
        System.out.println(str6.hashCode());//97

        String str7 = new String("b");//string object on heap
        String str8 = String.valueOf(str7);
        System.out.println(str7.equals(str8));//true
        System.out.println(str7 == str8);//true
        System.out.println(str7.hashCode());
        System.out.println(str8.hashCode());

        System.out.println("-----------------------");
        Integer integer1 = new Integer(1);// heap obj
        Integer integer2 = new Integer(1);//another heap obj
        System.out.println(integer1.equals(integer2));//true
        System.out.println(integer1 == integer2);//false
        System.out.println(integer1.hashCode());//1
        System.out.println(integer2.hashCode());//1

        Integer integer3 = 1;
        Integer integer4 = 1;
        System.out.println(integer3.equals(integer4));//true
        System.out.println(integer3==integer4);//true
        System.out.println(integer3.hashCode());//1
        System.out.println(integer4.hashCode());//1

        Integer integer5 = 128;
        Integer integer6 = 128;
        System.out.println(integer5.equals(integer6));//true
        System.out.println(integer5 == integer6);//false
        System.out.println(integer5.hashCode());//128
        System.out.println(integer6.hashCode());//128

        Integer integer7 = 127;
        Integer integer8 = Integer.valueOf(127);
        System.out.println(integer7.equals(integer8));//true
        System.out.println(integer7 == integer8);//true

        Integer integer9 = 128;
        Integer integer10 = Integer.valueOf(128);
        System.out.println(integer9.equals(integer10));//true
        System.out.println(integer9 == integer10);//false
    }
}

JDK版本:

panda@panda-e550:~$ java -version
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

原文:https://www.cnblogs.com/hapjin/p/10067875.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ArrayList底層使用時數組。LinkedList使用的是鏈表。 ArrayList: 數組查詢具有所有查詢特定元素比較快。而插入和刪除和修改比較慢(數組在記憶體中是一塊連續的記憶體,如果插入或刪除是需要移動記憶體)。 LinkedList: 鏈表不要求記憶體是連續的,在當前元素中存放下一個或上一個元素 ...
  • 題目內容: 你的程式要讀入一行文本,其中以空格分隔為若幹個單詞,以‘.’結束。你要輸出這行文本中每個單詞的長度。這裡的單詞與語言無關,可以包括各種符號,比如“it's”算一個單詞,長度為4。註意,行中可能出現連續的空格。 輸入格式: 輸入在一行中給出一行文本,以‘.’結束,結尾的句號不能計算在最後一 ...
  • 九大內置對象: 內置對象(又叫隱含對象),就是在jsp中,不需要創建(由伺服器<容器>來創建),可以直接使用的對象。 對象含義類作用域 request 請求對象 類型 javax.servlet.ServletRequest 作用域 Request response 響應對象 類型 javax.se ...
  • ClassLoader 做什麼的? 顧名思義,它是用來載入 Class 的。它負責將 Class 的位元組碼形式轉換成記憶體形式的 Class 對象。位元組碼可以來自於磁碟文件 *.class,也可以是 jar 包里的 *.class,也可以來自遠程伺服器提供的位元組流,位元組碼的本質就是一個位元組數組 []b ...
  • 1.for 迴圈 使用for語句可以遍歷全部元素,例如逐個輸出字元串中的字元,逐個輸出列表中的元素,元組中的元素,集合中的元素(註意賦值時各元素的順序),字典中的鍵……1-1.range迴圈: 這是從range生成的列表裡分別從0開始賦值給i變數,每賦值一次就迴圈一次,一直到列表結束"""" 元組的 ...
  • 【根索引】 【Python索引】 目標 使用PyCharm,完成兩個小實例的編寫和運行。一個是溫度轉換,一個是蟒蛇圖形繪製。 過程 1、先設置project目錄,雖然命名不是很正式,主要不太習慣軟體的目錄結構,好在只是熟悉語言和工具,就先把代碼都放一個目錄下吧。 2、可以打開多個py文件,運行時可以 ...
  • 問: 當用戶登錄某網站後,向伺服器發送一個請求,伺服器如何判斷是這個用戶請求的 首先,你要明白一點,最初http協議在設計的時候,主要面向當時的web1.0網站,他們不需要知道是誰來訪問,只需要向外界提供相應的web服務即可. 但是,在web2.0的網站中,用戶和網站發生相關的信息交互.所以,web ...
  • 方法:__name__ 、__doc__ 函數:wraps() 函數 裝飾器傳參、多個裝飾器,裝飾一個函數 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...