包装类的使用

包装类(Wrapper)

/*
* 包装类的使用
* 1.java提供了8种基本数据类型对象的包装类,使得基本数据类型的变量具有类的特征
* 2.掌握的:基本数据类型  String 包装类三者之间的转换
* */

在这里插入图片描述

基本数据类型 String 包装类三者之间的转换

基本数据类型–>包装类

package T1;

import org.junit.Test;


public class WrapperTest {
    //基本数据类型-->包装类   //调用包装类的构造器
    @Test
    public void test1(){
        int num1=10;
        Integer integer = new Integer(num1);
        System.out.println(num1==integer);  //true
        System.out.println(integer); //10
        Integer integer1 = new Integer("123");
        System.out.println(integer1);  //123
        System.out.println(123==integer1);  //true
	// Integer integer2 = new Integer("123abc");  //字符串里面必须是纯粹的数字,要不报异常

        Boolean aBoolean = new Boolean(true);
        System.out.println(aBoolean);//true
        Boolean aBoolean1 = new Boolean("true");
        System.out.println(aBoolean1);//true
        Boolean aBoolean2 = new Boolean("true123");
        System.out.println(aBoolean2); //false  看源码就明白了

    }
}
结果:
true
10
123
true
true
true
false

Process finished with exit code 0

Boolean中的源码

public Boolean(String s) {
        this(parseBoolean(s));
    }
//只要是true不论大小写都返回true,其他情况一律返回false
public static boolean parseBoolean(String s) {
        return ((s != null) && s.equalsIgnoreCase("true"));
    }
  • 看下面测试结果是什么
class Order{
    boolean isMale;
    Boolean isFemale;
}

测试单元

@Test
public void testWrapper(){
    Order order = new Order();
    System.out.println(order.isMale);  
    System.out.println(order.isFemale); 
}

结果:

false
null

因为类的默认值是null,基本数据类型中boolean中是false;

包装类–>基本数据类型

//包装类-->基本数据类型:调用包装类的xxxValue()
@Test
public void test2(){
    Integer in1 = new Integer(12);
    int i = in1.intValue();
    //包装类不可以直接加减运算(5.0后引入新特性:自动装箱与自动拆箱),转为基本类型了可以
    System.out.println(i+1);

    Float aFloat = new Float(12.3);
    float v = aFloat.floatValue();
    System.out.println(v+1);

}
结果:
13
13.3
  • 自动装箱与自动拆箱(5.0后的新特性)
@Test
public void test3(){
    //JDK5.0新特性
    //自动装箱  基本数据类型-->包装类
    int num2=10;
    Integer in1=num2;
    System.out.println(in1); //自动装箱   10

    boolean b1=true;
    Boolean b2 = b1;
    System.out.println(b2); //自动装箱  true

    //自动拆箱:包装类--->基本数据类型
    System.out.println(in1.toString());  //10
    int num3=in1;  //自动拆箱
    System.out.println(num3);  //10
}

基本数据类型、包装类—>String类型

//基本数据类型、包装类--->String类型 调用String重载的valueOf(xxx xxx)
@Test
public void test4(){
    int num1=10;
    //方式1,连接运算
    String str1=num1+"";
    //方式2
    float f1=12.3f;
    String s = String.valueOf(f1);
    System.out.println(s); //12.3

    Double aDouble = new Double(12.3);
    //传的是对象也没事,底层源码调用的还是该对象的toString
    String s1 = String.valueOf(aDouble);
    System.out.println(s1);

}
结果:
12.3
12.3

调用tring.valueOf(f1)走的方法源码是下面这个(返回的是包装类的toString())

public static String valueOf(float f) {
    return Float.toString(f);
}

调用String.valueOf(aDouble)走的方法源码是下面这个(最后还是走到了该包装类对象的toString)

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

String类型—>数据类型、包装类

  //String类型--->数据类型、包装类  调用包装类中的parseXxx()方法
    @Test
    public void test5(){
        String s = "123";
//        int num1=(int)s; //不可以,报错
//        Integer in1=(Integer) s;  //不可以
        int i = Integer.parseInt(s);  //返回的整形
        Integer j = Integer.parseInt(s);  //返回的是整形,然后自动装箱赋值给j
        System.out.println(i);  //123
        System.out.println(j);  //123

        String s1="TrUe";
        boolean b = Boolean.parseBoolean(s1);
        System.out.println(b);  //true
        String s11="true1";
        boolean b1 = Boolean.parseBoolean(s11);
        Boolean b11=Boolean.parseBoolean(s11);
        System.out.println(b1);  //false
        System.out.println(b11);  //false
    }
  • parseInt源码 返回值类型int
public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }
  • parseBoolean源码 返回值类型boolean
 public static boolean parseBoolean(String s) {
     							//只要是true就是false,忽略大小写							
        return ((s != null) && s.equalsIgnoreCase("true"));
    }

面试题

1. 猜猜结果是多少?

@Test
public void test1(){
    Object o1=true?new Integer(1):new Double(2.0);
    System.out.println(o1);
}

答案:1.0

因为在三元运算符中 表达式?a:b ; 要求a和b能同一为1个类型; 不统一会进行自动类型提升

2.这个呢?

@Test
public void test2(){
   Object o2;
   if(true){
       o2=new Integer(1);
   }else {
       o2=new Double(2.0);
   }
    System.out.println(o2); 
}

答案:1

这个就直接进if语句里面就行了

3.这个呢?

@Test
public void test3(){
    Integer m=new Integer(1);
    Integer n=new Integer(1);
    System.out.println(m==n);

    Integer m1=new Integer(128);
    Integer n1=new Integer(128);
    System.out.println(m1==n1);
    
    Integer i=1;
    Integer j=1;
    System.out.println(i==j);
    
    Integer d=128;
    Integer a=128;
    System.out.println(a==d);
}

结果:

false
false
true
false

因为Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],

保存了从-128到127范围的整数,如果我们使用自动装箱的方式,给Interger赋值的范围在-128 到127范围内时,可以直接使用数组中的元素,不用再去new了.不再范围内的则会去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() {}
}
   }
        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() {}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值