成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

包裝類的valueOf

fox_soyoung / 610人閱讀

摘要:序這里記錄下幾個(gè)包裝類的方法,備忘下。注意如果傳入的值在之間,則返回,否則返回跟沒有

這里記錄下幾個(gè)包裝類的valueOf方法,備忘下。

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
     */
    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

注意static final int low = -128;
如果傳入的int值在-128-127之間,則返回cache,否則返回new

Long.valueOf
/**
     * Returns a {@code Long} instance representing the specified
     * {@code long} value.
     * If a new {@code Long} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Long(long)}, as this method is likely to yield
     * significantly better space and time performance by caching
     * frequently requested values.
     *
     * Note that unlike the {@linkplain Integer#valueOf(int)
     * corresponding method} in the {@code Integer} class, this method
     * is not required to cache values within a particular
     * range.
     *
     * @param  l a long value.
     * @return a {@code Long} instance representing {@code l}.
     * @since  1.5
     */
    public static Long valueOf(long l) {
        final int offset = 128;
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
Boolean.valueOf
/**
     * Returns a {@code Boolean} instance representing the specified
     * {@code boolean} value.  If the specified {@code boolean} value
     * is {@code true}, this method returns {@code Boolean.TRUE};
     * if it is {@code false}, this method returns {@code Boolean.FALSE}.
     * If a new {@code Boolean} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Boolean(boolean)}, as this method is likely to yield
     * significantly better space and time performance.
     *
     * @param  b a boolean value.
     * @return a {@code Boolean} instance representing {@code b}.
     * @since  1.4
     */
    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }
    /**
     * Returns a {@code Boolean} with a value represented by the
     * specified string.  The {@code Boolean} returned represents a
     * true value if the string argument is not {@code null}
     * and is equal, ignoring case, to the string {@code "true"}.
     *
     * @param   s   a string.
     * @return  the {@code Boolean} value represented by the string.
     */
    public static Boolean valueOf(String s) {
        return toBoolean(s) ? TRUE : FALSE;
    }
Byte.valueOf
/**
     * Returns a {@code Byte} instance representing the specified
     * {@code byte} value.
     * If a new {@code Byte} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Byte(byte)}, as this method is likely to yield
     * significantly better space and time performance since
     * all byte values are cached.
     *
     * @param  b a byte value.
     * @return a {@code Byte} instance representing {@code b}.
     * @since  1.5
     */
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

Float跟Double沒有cache

Float.valueOf
/**
     * Returns a {@code Float} instance representing the specified
     * {@code float} value.
     * If a new {@code Float} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Float(float)}, as this method is likely to yield
     * significantly better space and time performance by caching
     * frequently requested values.
     *
     * @param  f a float value.
     * @return a {@code Float} instance representing {@code f}.
     * @since  1.5
     */
    public static Float valueOf(float f) {
        return new Float(f);
    }
Double.valueOf
/**
     * Returns a {@code Double} instance representing the specified
     * {@code double} value.
     * If a new {@code Double} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Double(double)}, as this method is likely to yield
     * significantly better space and time performance by caching
     * frequently requested values.
     *
     * @param  d a double value.
     * @return a {@code Double} instance representing {@code d}.
     * @since  1.5
     */
    public static Double valueOf(double d) {
        return new Double(d);
    }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65741.html

相關(guān)文章

  • JAVA學(xué)習(xí)之路 (九)包裝

    摘要:包裝類基本數(shù)據(jù)類型如等。它們并不具備對(duì)象的特性,比如不能調(diào)用方法。為了讓基本數(shù)據(jù)類型也能具有對(duì)象的特性,為每個(gè)基本數(shù)據(jù)類型提供了包裝類。 包裝類 基本數(shù)據(jù)類型:如 int、float、double、boolean、char 等。它們并不具備對(duì)象的特性,比如不能調(diào)用方法。為了讓基本數(shù)據(jù)類型也能具有對(duì)象的特性,java為每個(gè)基本數(shù)據(jù)類型提供了包裝類。 基本類型和包裝類之間的對(duì)應(yīng)關(guān)系: sh...

    MockingBird 評(píng)論0 收藏0
  • java面向?qū)ο?中)

    摘要:使用創(chuàng)建的字符串對(duì)象是運(yùn)行時(shí)創(chuàng)建出來的,它被保存在運(yùn)行時(shí)內(nèi)存區(qū),即堆內(nèi)存,不會(huì)放入常量池中。類成員創(chuàng)建的對(duì)象實(shí)例中根本不會(huì)擁有對(duì)應(yīng)類的類變量。抽象類的構(gòu)造器不能用于創(chuàng)建實(shí)例,主要是用于被其子類調(diào)用。 Java提供了final關(guān)鍵字來修飾變量,方法和類,系統(tǒng)不允許為final變量重新賦值,子類不允許覆蓋父類的final方法,final類不能派生子類。 Abstract 和 inte...

    孫吉亮 評(píng)論0 收藏0
  • 深入淺出 Java 中的包裝

    摘要:前陣子,我們分享了中的基本數(shù)據(jù)類型轉(zhuǎn)換這篇文章,對(duì)許多粉絲還是有帶來幫助的,今天講一下包裝類的的由來,及自動(dòng)裝箱拆箱的概念和原理。下面是基本數(shù)據(jù)類型與對(duì)應(yīng)的包裝類型。 showImg(https://segmentfault.com/img/remote/1460000016537706); 前陣子,我們分享了《Java中的基本數(shù)據(jù)類型轉(zhuǎn)換》這篇文章,對(duì)許多粉絲還是有帶來幫助的,今天講...

    ytwman 評(píng)論0 收藏0
  • 第八章-Java常用API#yyds干貨盤點(diǎn)#

    摘要:常用類概述包含執(zhí)行基本數(shù)字運(yùn)算的方法沒有構(gòu)造方法,如何使用類中的成員呢看類的成員是否都是靜態(tài)的,如果是,通過類名就可以直接調(diào)用。所有類都直接或間接的繼承該類。 1 常用API1.1 Math1.1.1 Math類概述Math包含執(zhí)行基本數(shù)字運(yùn)算的方法沒有構(gòu)造方法,如何使用類中的成員呢?看類的成員是否都是靜態(tài)的,...

    番茄西紅柿 評(píng)論0 收藏2637
  • Java 面向?qū)ο螅ㄏ拢?/b>

    摘要:換句話說,一共產(chǎn)生了兩個(gè)字符串對(duì)象。類成員屬于整個(gè)類,而不屬于單個(gè)對(duì)象。類變量生存范圍幾乎等同于該類的生存范圍。當(dāng)通過對(duì)象來訪問類變量時(shí),系統(tǒng)會(huì)在底層轉(zhuǎn)換為通過該類來訪問類變量。 Java8增強(qiáng)的包裝類 showImg(https://segmentfault.com/img/bVFyHX?w=917&h=276);自動(dòng)裝箱:把一個(gè)基本類型變量直接賦給對(duì)應(yīng)的包裝類變量,或者賦給Obje...

    nanchen2251 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<