Java封装类总结(个人学习总结,不喜勿喷!!!!)

目录

一、包装类Wrapper

1.1.每一个基本类型都有与之对应的对象(包装类型),所谓的面向对象编程!操作对象一定比操作基本类型简单因为对象提供了强大的功能供我们使用。

1.2.java一共有8种基本数据类型,分别为:byte、short、int、long、float、double、char、boolea。其对应的对象(包装类型)分别为:Byte、Short、Integer、Long、Float、Double、Character、Boolean。

 二、转换关系

2.1.转换关系:

2.2自动装箱和拆箱(autoboxing)

三、代码示例


一、包装类Wrapper

1.1.每一个基本类型都有与之对应的对象(包装类型),所谓的面向对象编程!操作对象一定比操作基本类型简单因为对象提供了强大的功能供我们使用。

1.2.java一共有8种基本数据类型,分别为:byte、short、int、long、float、double、char、boolea。其对应的对象(包装类型)分别为:Byte、Short、Integer、Long、Float、Double、Character、Boolean。

 二、转换关系

2.1.转换关系:

基本类型-> 包装类  Integer.valueOf(int i)
包装类-> 基本类型  x.intValue()
基本类型 -> String  String.valueOf(String s)
String ->基本类型   Integer.parseInt(Sting s)
包装类 ->String      integer2.toString();
String -> 包装类    Integer.valueOf(s2);

2.2自动装箱和拆箱(autoboxing)

三、代码示例

public class TestWrapper {
//    主方法:程序的入口 -> 读代码的入口,看一个代码从main的第一行开始看起。
    public static void main(String[] args) {
//    1.基本类型 -> 包装类
        int i =100;
        Integer integer = Integer.valueOf(i);
        System.out.println(integer);

//        2.包装类 -> 基本类型
        Integer integer1 =100;
        int i1 =integer1.intValue();
        System.out.println(i1);

//        3.基本类型 -> String
        String s = String.valueOf(i);
        System.out.println(s);

        //!!!4.String -> 基本类型
        String s1 ="123";
        int i2 = Integer.parseInt(s1);
        System.out.println(i2);

        //5. String -> 包装类
        String s2 ="12333";
        Integer integer3 = Integer.valueOf(s2);
        System.out.println(integer3);


        //6. 包装类 -> String
        Integer integer2 =1233;
        String s3 = integer2.toString();
        System.out.println(s3);


        //7.autoboxing:基本类型和包装类之间的转换的自动完成的。
        //直接写出来的数字默认为int,直接写出来的小数默认为double。
        Integer x =100; //  Integer .valueof(100);自动装箱
        Integer y =200; //  Integer .valueof(200);自动装箱
        /*
        int x = x.intValue();//自动拆箱
        int y = y.intValue();//自动拆箱
        int z =x+y;
        Integer z = Integer.valueOf(z);//自动装箱
        * */
        Integer z =x+y;//
        System.out.println(z);//300


    }
}

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

练习时常两年半的Java练习生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值