包装类使用

1.Java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特性

 

2.掌握:基本数据类型、包装类、String三者之间的相互转换

 

string类型---->基本数据类型、包装类:调用包装类的parseXxx(String s)

String str1 = "123";
int num2 = Integer.parseInt(str1)
sysout(num2+1)
String str2 = "true1";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
//错误的情况:
//      int num1 = (int)str1;
//      Integer in1 = (Integer)str1;//没有子父类的关系不能强转
//可能会报NumberFormatException

基本数据类型、包装类---->String类型:调用String重载的valueOf(Xxx xxx)

int num1 = 10;
String str1 = num1 + "";//基本数据类型转String类型最简单的方法!
float f1 = 12.3f;
String str2 = String.valueOf(f1);//输出  “12.3”
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str3);//"12.4"

自动装箱和自动拆箱(就可以把包装类和基本数据类型看出一类了,后面的方法也可以不管了)

        int num1 = 10;
        //基本数据类型-->包装类的对象
        method(num1);//这里要把int型的num1给object,显然这是不能直接转换的,但是编译器就可以,这里就是自动的装箱了!!
        
        //自动装箱:基本数据类型 --->包装类
        int num2 = 10;
        Integer in1 = num2;//自动装箱
        
        boolean b1 = true;
        Boolean b2 = b1;//自动装箱
        
        //自动拆箱:包装类--->基本数据类型
        System.out.println(in1.toString());
        
        int num3 = in1;//自动拆箱
public void method(Object obj){
    System.out.println(obj);
}

基本数据类型----->包装类:调用包装类的构造器

整形--->包装类
int num1 = 10;
Integer in1 = new Integer(num1);//调用包装类的构造器
System.out.println(in1.toString());//10
------------------------------------------------------------------------------------------------------------
字符--->包装类
Integer in2 = new Integer("123");
System.out.println(in2.toString());//123
------------------------------------------------------------------------------------------------------------
Integer in3 = new Integer("123abc"); //异常情况    
System.out.println(in3.toString());      
------------------------------------------------------------------------------------------------------------
浮点型--->包装类
Float f1 = new Float(12.3f);
Float f2 = new Float("12.3");
System.out.println(f1);//12.3
System.out.println(f2);//12.3
------------------------------------------------------------------------------------------------------------
布尔型--->包装类
Boolean b1 = new Boolean(true);
System.out.println(b1);
字符--->包装类
Boolean b2 = new Boolean("TrUe");
System.out.println(b2);
Boolean b3 = new Boolean("true123");
System.out.println(b3);//false
总结:字符串转化为布尔型的包装类时,非true(忽略大小写)都为false
------------------------------------------------------------------------------------------------------------
Order order = new Order();
System.out.println(order.isMale);//false,默认值
System.out.println(order.isFemale);//null
class Order{
    
    boolean isMale;
    Boolean isFemale;
}
这里需要注意的就是isFemale现在是被包装类布尔所声明的,他现在是一个类,所以就为空
------------------------------------------------------------------------------------------------------------
​

包装类---->基本数据类型:调用包装类Xxx的xxxValue()

Integer in1 = new Integer(12);
int i1 = in1.intValue();//**
System.out.println(i1 + 1);//13
------------------------------------------------------------------------------------------------------------
Float f1 = new Float(12.3);
float f2 = f1.floatValue();
System.out.println(f2 + 1);//13.3

课后题目

/*
 * 关于包装类使用的面试题
 * 
 * 
 */
public class InterviewTest {
    @Test
    public void test1() {
        Object o1 = true ? new Integer(1) : new Double(2.0);
        System.out.println(o1);// 1.0
    //是因为程序执行一条语句时,不是只看一部分,所以会自动类型提升
    }
    @Test  
    public void test2() {
        Object o2;
        if (true)
            o2 = new Integer(1);
        else
            o2 = new Double(2.0);
        System.out.println(o2);// 1
​
    }
​
    @Test
    public void test3() {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j);//false,比地址
        
        //Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],
        //保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在
        //-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率
        
        Integer m = 1;
        Integer n = 1;
        System.out.println(m == n);//true
​
        Integer x = 128;//相当于new了一个Integer对象
        Integer y = 128;//相当于new了一个Integer对象
        System.out.println(x == y);//false
    }
​
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值