摘要:序這里記錄下幾個包裝類的方法,備忘下。注意如果傳入的值在之間,則返回,否則返回跟沒有
序
這里記錄下幾個包裝類的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
/** * 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.valueOfFloat跟Double沒有cache
/** * 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); }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65741.html
摘要:包裝類基本數據類型如等。它們并不具備對象的特性,比如不能調用方法。為了讓基本數據類型也能具有對象的特性,為每個基本數據類型提供了包裝類。 包裝類 基本數據類型:如 int、float、double、boolean、char 等。它們并不具備對象的特性,比如不能調用方法。為了讓基本數據類型也能具有對象的特性,java為每個基本數據類型提供了包裝類。 基本類型和包裝類之間的對應關系: sh...
摘要:使用創建的字符串對象是運行時創建出來的,它被保存在運行時內存區,即堆內存,不會放入常量池中。類成員創建的對象實例中根本不會擁有對應類的類變量。抽象類的構造器不能用于創建實例,主要是用于被其子類調用。 Java提供了final關鍵字來修飾變量,方法和類,系統不允許為final變量重新賦值,子類不允許覆蓋父類的final方法,final類不能派生子類。 Abstract 和 inte...
摘要:前陣子,我們分享了中的基本數據類型轉換這篇文章,對許多粉絲還是有帶來幫助的,今天講一下包裝類的的由來,及自動裝箱拆箱的概念和原理。下面是基本數據類型與對應的包裝類型。 showImg(https://segmentfault.com/img/remote/1460000016537706); 前陣子,我們分享了《Java中的基本數據類型轉換》這篇文章,對許多粉絲還是有帶來幫助的,今天講...
摘要:常用類概述包含執行基本數字運算的方法沒有構造方法,如何使用類中的成員呢看類的成員是否都是靜態的,如果是,通過類名就可以直接調用。所有類都直接或間接的繼承該類。 1 常用API1.1 Math1.1.1 Math類概述Math包含執行基本數字運算的方法沒有構造方法,如何使用類中的成員呢?看類的成員是否都是靜態的,...
摘要:換句話說,一共產生了兩個字符串對象。類成員屬于整個類,而不屬于單個對象。類變量生存范圍幾乎等同于該類的生存范圍。當通過對象來訪問類變量時,系統會在底層轉換為通過該類來訪問類變量。 Java8增強的包裝類 showImg(https://segmentfault.com/img/bVFyHX?w=917&h=276);自動裝箱:把一個基本類型變量直接賦給對應的包裝類變量,或者賦給Obje...
閱讀 3768·2021-08-30 09:47
閱讀 3690·2019-08-30 15:56
閱讀 677·2019-08-30 14:18
閱讀 698·2019-08-29 16:17
閱讀 2065·2019-08-29 11:07
閱讀 642·2019-08-26 13:53
閱讀 3443·2019-08-26 10:26
閱讀 2491·2019-08-23 18:30