资讯专栏INFORMATION COLUMN

包装类的valueOf

fox_soyoung / 582人阅读

摘要:序这里记录下几个包装类的方法,备忘下。注意如果传入的值在之间,则返回,否则返回跟没有

这里记录下几个包装类的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);
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65741.html

相关文章

  • JAVA学习之路 (九)包装

    摘要:包装类基本数据类型如等。它们并不具备对象的特性,比如不能调用方法。为了让基本数据类型也能具有对象的特性,为每个基本数据类型提供了包装类。 包装类 基本数据类型:如 int、float、double、boolean、char 等。它们并不具备对象的特性,比如不能调用方法。为了让基本数据类型也能具有对象的特性,java为每个基本数据类型提供了包装类。 基本类型和包装类之间的对应关系: sh...

    MockingBird 评论0 收藏0
  • java面向对象(中)

    摘要:使用创建的字符串对象是运行时创建出来的,它被保存在运行时内存区,即堆内存,不会放入常量池中。类成员创建的对象实例中根本不会拥有对应类的类变量。抽象类的构造器不能用于创建实例,主要是用于被其子类调用。 Java提供了final关键字来修饰变量,方法和类,系统不允许为final变量重新赋值,子类不允许覆盖父类的final方法,final类不能派生子类。 Abstract 和 inte...

    孙吉亮 评论0 收藏0
  • 深入浅出 Java 中的包装

    摘要:前阵子,我们分享了中的基本数据类型转换这篇文章,对许多粉丝还是有带来帮助的,今天讲一下包装类的的由来,及自动装箱拆箱的概念和原理。下面是基本数据类型与对应的包装类型。 showImg(https://segmentfault.com/img/remote/1460000016537706); 前阵子,我们分享了《Java中的基本数据类型转换》这篇文章,对许多粉丝还是有带来帮助的,今天讲...

    ytwman 评论0 收藏0
  • 第八章-Java常用API#yyds干货盘点#

    摘要:常用类概述包含执行基本数字运算的方法没有构造方法,如何使用类中的成员呢看类的成员是否都是静态的,如果是,通过类名就可以直接调用。所有类都直接或间接的继承该类。 1 常用API1.1 Math1.1.1 Math类概述Math包含执行基本数字运算的方法没有构造方法,如何使用类中的成员呢?看类的成员是否都是静态的,...

    番茄西红柿 评论0 收藏2637
  • Java 面向对象(下)

    摘要:换句话说,一共产生了两个字符串对象。类成员属于整个类,而不属于单个对象。类变量生存范围几乎等同于该类的生存范围。当通过对象来访问类变量时,系统会在底层转换为通过该类来访问类变量。 Java8增强的包装类 showImg(https://segmentfault.com/img/bVFyHX?w=917&h=276);自动装箱:把一个基本类型变量直接赋给对应的包装类变量,或者赋给Obje...

    nanchen2251 评论0 收藏0

发表评论

0条评论

fox_soyoung

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<