博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中封装类型.valueOf()
阅读量:4518 次
发布时间:2019-06-08

本文共 3558 字,大约阅读时间需要 11 分钟。

@Test    public void test() {        Integer i3 =128;        Integer i4=128;        boolean integerGT127=i3==i4;         //false        System.out.println("Integer 128==128"+integerGT127);        Integer i1 =127;        Integer i2=127;        boolean integerLt127=i1==i2;       //true        System.out.println("Integer 127==127"+integerLt127);        Integer i5 = 5001;        boolean unBoxingInteger=i5 > 500 && i5 < 1000;        System.out.println("Integer unbox "+integerLt127);        Integer i6=500,i7=1000;        boolean unBoxingInteger2=i5.compareTo(i6)>0&&i5.compareTo(i7)<0;        System.out.println("Integer compare "+integerLt127);        //装箱 将 10(int)转换为了 Integer        Integer i9=10;        //拆箱 将i9(Integer)转换为了int        int i10=i9;            }

输出结果:

1 .Integer 128==128false

2 .Integer 127==127true
3 .Integer unbox true
4 .Integer compare true

为什么会出现128==128的结果?输出结果表明i1和i2指向的是同一个对象,而i3和i4指向的是不同的对象。IntegerCache 下面这段代码是Integer的valueOf方法的具体实现:

private static class IntegerCache {        static final int low = -128;        static final int high;        static final Integer cache[];        static {            // high value may be configured by property            int h = 127;            String integerCacheHighPropValue =                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");            if (integerCacheHighPropValue != null) {                try {                    int i = parseInt(integerCacheHighPropValue);                    i = Math.max(i, 127);                    // Maximum array size is Integer.MAX_VALUE                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);                } catch( NumberFormatException nfe) {                    // If the property cannot be parsed into an int, ignore it.                }            }            high = h;            cache = new Integer[(high - low) + 1];            int j = low;            for(int k = 0; k < cache.length; k++)                cache[k] = new Integer(j++);            // range [-128, 127] must be interned (JLS7 5.1.7)            assert IntegerCache.high >= 127;        }        private IntegerCache() {}    }

在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

Integer.valueOf(10)

public static Integer valueOf(int i) {        if (i >= IntegerCache.low && i <= IntegerCache.high)            return IntegerCache.cache[i + (-IntegerCache.low)];        return new Integer(i);    }
 
Integer.valueOf(10); Double.valueOf(23.00); Boolean.valueOf(true); Boolean b1=true,b2=true; //true  Boolean.valueOf(true); // final修饰的对象只能指向唯一一个对象,不可以再将它指向其他对象,而static final修饰的对象则可以使一个常量真正做到不被修改 System.out.println(b1==b2); // true  Boolean.valueOf(true); System.out.println(b1.equals(b2)); Double d1=20.00,d2=20.00; // false   Double.valueOf System.out.println(d1==d2); //true System.out.println(d1.equals(d2));
Boolean.valueOf(true);返回了一个static final 的对象 ,final修饰的对象只能指向唯一一个对象,不可以再将它指向其他对象,而static final修饰的对象则可以使一个常量真正做到不被修改
public static Boolean valueOf(boolean b) {        return (b ? TRUE : FALSE);    }    /**     * The {
@code Boolean} object corresponding to the primitive * value {
@code true}. */ public static final Boolean TRUE = new Boolean(true); /** * The {
@code Boolean} object corresponding to the primitive * value {
@code false}. */ public static final Boolean FALSE = new Boolean(false);

 

Double.valueOf(23.00);new一个新的对象
public static Double valueOf(double d) {        return new Double(d);    }

 

转载于:https://www.cnblogs.com/fanBlog/p/10764543.html

你可能感兴趣的文章
NOR Flash的原理与操作
查看>>
javaweb学习总结(二十六)——jsp简单标签标签库开发(二)
查看>>
Could not open Hibernate Session for transaction, 数据库连接超时解决方法
查看>>
C# UrlEncoding
查看>>
关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
查看>>
sqoop安装及使用
查看>>
(10)JavaScript学习笔记 - 数组
查看>>
android4.4之后的HttpUrlConnection的实现是基于okhttp
查看>>
idea tomcat启动乱码问题
查看>>
日历插件bootstrap-datetimepicker的使用感悟
查看>>
vue具体页面跳转传参方式
查看>>
look与look like
查看>>
ERP实施顾问面试技巧(转载)
查看>>
Page_Load基类,重写OnLoad
查看>>
TeamWork#3,Week5,Bing Input Method vs Sogou Input Method
查看>>
计算机核心期刊排名及投稿信息
查看>>
ALGO-84 大小写转换
查看>>
github 中redisPhpAdmin redis 可视化界面
查看>>
Codeforces - 1176E - Cover it! - bfs
查看>>
Win10系列:C#应用控件进阶3
查看>>