javase -- 包装类


包装类(Wrapper).

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

Byte,Short,Integer,Long,Float,Double --> 父类:Number

基本数据类型、包装类和String类型之间的装换.

基本类型 <==> 包装类

// 基本 ==> 包装
//1. 构造器
Integer i = new Integer(100);
//2. Integer.valueOf
Integer j = Integer.valueOf(100);//----------------- 自动装箱默认调用
//2. 自动装箱
Integer k = 100;
Integer i = 100;
//包装 ==> 基本
//1. intValue()
int j = i.intValue(); // ----------- 自动拆箱默认调用
//2. 自动拆箱
int k = i;

基本类型 <==> String类型

//基本 ==> String
//1. String.valueOf();
String str1 = String.valueOf(100);// ----------
//2. String 连接运算符 +
String str2 = "" + 100;// ------------
//3. Integer.toString
String str3 = Integer.toString(100);
//String ==> 基本类型
//1. Integer.parseInt()
int i = Integer.parseInt("100");// -------------
//2. 通过包装类构造器 --> 自动拆箱
int j = new Integer("100");

包装类 <==> String类型

Integer i = 100;
//包装 ==> String
//1. 对象.toString
String str1 = i.toString();// --------------------
//2. Integer.toString
String str2 = Integer.toString(i);
//String ==> 包装
//1. 构造器
Integer i = new Integer("100");
//2. Integer.valueOf();
Integer j = Integer.valueOf("100");

Integer特有Integer k = Integer.getInstance("100");

自动拆箱 和 自动装箱.

JDK1.5

自动装箱:

int i = 1;
Integer j = i;//自动装箱

自动拆箱:

Integer i = new Integer(1);
int j = i;//自动拆箱

面试题.

1..

Object o = true ? new Integer(1) : new Double(2.0); 
System.out.println(o);//? == 1.0 因为 int 与 double 一起运算 类型会别提升
Object o;
if(true)
    o = new Integer(1);
else
    o = new Double(2.0);
System.out.println(o);//? == 1 

2..

Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);// false
Integer i = 1;
Integer j = 1;
System.out.println(i == j);// true
Integer i = 128;//相当于new了一个对象
Integer j = 128;//相当于new了一个对象
System.out.println(i == j);// false
  • 为什么第二个为true、第三个却为false

    因为在Integer类里面存在IntegerCache这么一个内部类,它用于缓存-128~127的整型数值,自动装箱时,会先到这个缓存中找 这个范围的数值,不在范围内才new

    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() {}
    }

文章作者: liuminkai
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 liuminkai !
评论
 上一篇
javase -- static关键字 javase -- static关键字
static. 静态的 可以用于修饰属性、方法、代码块、内部类 static修饰属性:静态变量(类变量). 属性除了静态变量,就是实例变量 一个类的多个对象共享同一个静态变量 随着类的加载而加载 静态变量的加载早于对象的创建
2020-07-30
下一篇 
javase -- 向下转型 javase -- 向下转型
向上转型(多态性).Person p = new Men(); 向下转型(强制转换). 注意对象p 本质上仍然是 Men Men m = (Men)p; 错误示范.示范一: 兄弟类型 //Woman 和 Men 直接父类都是 Person
2020-07-29
  目录