Java 包装类知识总结

1.基本类型和包装类对应关系

除了int和char例外,其他均首字母大写

基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
char Charater
float Float
double Double
boolean Bloolean

2.基本类型和包装类转换

  • 自动装箱和拆箱

    • 自动装箱: 基本类型-->包装类
      {
          //1.直接把基本变量赋值给Integer对象
          Integer intObj = 5;
          //2.直接把基本变量赋值给Object对象
          Object boolObject = true;
      }
    
    

 - **自动拆箱:** 基本类型<--包装类
 ```Java
   {
       //1.直接把Integer赋值给int
       int it = intObj;
       
       //2.先把Object强制转换成Boolean,再赋值给boolean
       if (boolObject instanceof Boolean){
           boolean b = (Boolean)boolObject;
       }
   }
 

注意: 自动拆箱和装箱只能在对应类型之间,例如int和Integer。

3.包装类和字符串直接转换

  • 字符串转化成基本类型
    {
        String intStr = "111";
        
        //1.利用包装类提供的Xxx(String x)构造器
        int int1 = new Integer(intStr);
        
        //2.利用包装类提供的parseXxx(String x)静态方法
        int int2 = Integer.parseInt(intStr);
    }
    

注意: Character包装类无此parseXxx(String x)静态方法

  • 基本类型转化成字符串
    {
        int it1 = 111;
        
        //1.通过String.valueOf(xxx)转换
        String str1 = String.valueOf(int1);
        
        //2.将基本变量和""进行连接运算
        String str2 = it1 + "";
    }
    

4.包装类,基本变量相互比较

  • 包装类和基本变量比较

    可以直接进行数值比较

    {
        Integer a = new Integer(100);
        //输出true
        System.out.println("100的包装类实力是否大于50:"+(a > 50));
    }
    
  • 包装类直接相互比较

    • 包装类实例实际是引用类型,只有两个包装类引用同一个对象时才会返回true

      {
          //输出false
          System.out.println("比较100的包装类是否相等:"+
          (new Integer(100) == new Integer(100)));
      }
      
    • 特殊情况(部分面试会问): 自动装箱可以把一些基本类型赋值给一个包装类实例,但会有以下特殊情况~

      public static void main(String[] args)
      {
      	
          //通过自动装箱,允许把2包装成包装类型
      	Integer ina = 2;
      	Integer inb = 2;
      	System.out.println("两个2自动装箱后是否相等"
      		+ (ina == inb)); // 输出true
      	Integer biga = 128;
      	Integer bigb = 128;
      	System.out.println("两个2自动装箱后是否相等"
      		+ (biga == bigb)); // 输出false
      }
      

      直接看源码分析:
      系统会把一个-128~127之间的数值自动装箱成Integer实例,并放入cache数组缓存起来,如果以后还有这个范围的整数自动装箱,实际上是指向同一个数组元素,但不在这个范围内的,系统会重新创建一个Integer实例。

      
      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 =
                  sun.misc.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() {}
      }
      
      public static Integer valueOf(int i) {
          if (i >= IntegerCache.low && i <= IntegerCache.high)
              return IntegerCache.cache[i + (-IntegerCache.low)];
          return new Integer(i);
      }
      
      

同上,也使用cache的如下面列表所示:

包装类 缓存数组范围
Byte static final Byte cache[] = new Byte[-(-128) + 127 + 1];
Short static final Short cache[] = new Short[-(-128) + 127 + 1]
Long static final Long cache[] = new Long[-(-128) + 127 + 1];
Character static final Character cache[] = new Character[127 + 1];
Float
Double
Boolean
posted @ 2016-12-29 13:37  我爱海贼王  阅读(186)  评论(0)    收藏  举报