zoukankan      html  css  js  c++  java
  • 包装器类型

    1.基本数据类型和包装器类型
    包装器类型都重写了equals方法
    boolean   Boolean
    byte          Byte
    short         Short
    char           Character
    int              Integer
    long           Long
    float           Float
    double       Double
    2.有了基本类型为什么还要有包装类型
    基本类型并不具有对象的性质,为了让基本类型也具有对象的特征,就出现了包装类型(我们在使用集合类型Collection时就一定要使用包装类型而非基本类型)
    当需要往ArrayList,HashMap中放东西时,像int,double这种基本类型是放不进去的,因为容器都是装object的,这是就需要这些基本类型的包装器类。
    3.相互转换
    int i = 0;  
    Integer ii = new Integer(i);//int类型转换为Integer类型

    Integer ii = new Integer(0);  
    int i = ii.intValue();  //Integer转换为int类型
    基本类型是直接将变量值存储在栈中,而包装类型是将对象放在堆中,然后通过引用来使用;
    4.自动装箱和自动拆箱
    Integer i1 = 128;  ---->  Integer i1 = Integer.valueOf(128);
    自动装箱 Int类型强转成Integer
    int a2 = i2;       ---->  int a2 = i2.intValue();
    自动拆箱 Integer类型强转成Int
     自动装箱的时候,如果这个数字在[-128~127]之间那么就是从缓存中获取数据。
                     如果这个数字不在范围内,那么就是利用new关键字创建Integer对象

    5.问题一:Integer a =  128 ,  Integer b = 128;a == b ?--->true/false   false
                 Integer a =  127 ,  Integer b = 127;a == b ?--->true/false     true
    Integer类缓存了-128-127之间的数,在这之间的数直接放到缓存池,超过128的Integer值
    都是new出来的对象,比较两个对象的地址值肯定不相同。
    6.intValue(): 以 int 类型返回该 Integer 的值。即返回的是栈中 字面量同的地址。
    7.什么时候会发生自动装箱/拆箱问题
     当int 和 Integer互相转换的时候回发生。

    什么时候自动拆箱和自动装箱不起作用?

    Integer  int作为方法的参数发生重载时。
    8.Integer.parseInt("全数字字符串");

  • 相关阅读:
    hdu 1024 Max Sum Plus Plus DP
    九月回顾 这篇文章和ACM毫无关系= =
    HDU 3974 Assign the task 并查集/图论/线段树
    poj 3264 Balanced Lineup RMQ问题
    zoj 1610 Count the Colors 线段树区间更新/暴力
    poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
    hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和
    hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
    codeforces 19D D. Points 树套树
    codeforces 85D D. Sum of Medians Vector的妙用
  • 原文地址:https://www.cnblogs.com/yxj808/p/11968474.html
Copyright © 2011-2022 走看看