摘要:序這里記錄下幾個(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
/** * 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); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65741.html
摘要:包裝類基本數(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...
摘要:使用創(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...
摘要:前陣子,我們分享了中的基本數(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ì)許多粉絲還是有帶來幫助的,今天講...
摘要:常用類概述包含執(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)的,...
摘要:換句話說,一共產(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...
閱讀 3782·2021-08-30 09:47
閱讀 3719·2019-08-30 15:56
閱讀 684·2019-08-30 14:18
閱讀 704·2019-08-29 16:17
閱讀 2071·2019-08-29 11:07
閱讀 649·2019-08-26 13:53
閱讀 3456·2019-08-26 10:26
閱讀 2502·2019-08-23 18:30