当前位置: 首页 > article >正文

【JAVA学习笔记】48 - 八大常用Wrapper类(包装类)

项目代码

https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter13/src/com/yinhai/wrapper_

内的wrapper

一、包装类

1.针对八种基本定义相应的引用类型一包装类

2.有了类的特点,就可以调用类中的方法。


黄色背景的表示父类是Number

 二、包装类和基本数据的转换

演示包装类和基本数据类型的相互转换,这里以int和Integer演示。

1.jdk5前的手动装箱和拆箱方式,箱: 基本类型-> 包装类型 反之,拆箱

2.jdk5以后(含jdk5)的自动装箱和拆箱方式

3.自动装箱底层调用的是valueOf方法,比如Integer.valueOf(),自动拆箱用的是Value方法,例如intValue();

public class Integer01 {
    public static void main(String[] args) {
        //演示int -- Integer的装箱和拆箱
        //手动装箱
        int n1 = 100;
        Integer integer = new Integer(n1);
        Integer integer1 = Integer.valueOf(n1);
        //手动拆箱
        //Integer -> int
        int i = integer.intValue();

        //jdk5以后就可以自动装箱自动拆箱了
        int n2 = 200;
        //自动装箱
        Integer integer2 = n2;//底层是使用了Interger.valueOf(n2),使用debug能看到走到valueOf;
        int n3 = integer2;//底层仍然是使用了方法integer.intValue()使用debug能看到走到intValue
    }
}

三、包装类的课堂练习

1.

第一段自动装箱

第二段常true执行冒号前的,动态绑定用Integer的toString方法

返回的为传入的值,所以输出1

不是1,三元运算符应该看做一个整体,所以会自动转换提升至最高的Double

所以输出的是1.0 

第三段同理删除线 1

2.

【疑问解决】在自动装箱中Integer赋予一个常量1,为什么会出现==判断true和flase的情况(JDK源码、内部缓冲)-CSDN博客

public class WrapperExercise02 {
    public static void main(String[] args) {
        Integer i = new Integer(1);
        Integer j = new Integer(1);
        System.out.println(i == j);  //False
        //所以,这里主要是看范围 -128 ~ 127 就是直接返回
        /*
        //1. 如果i 在 IntegerCache.low(-128)~IntegerCache.high(127),就直接从数组返回
        //2. 如果不在 -128~127,就直接 new Integer(i)
         public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
         */
        Integer m = 1; //底层 Integer.valueOf(1); -> 阅读源码
        Integer n = 1;//底层 Integer.valueOf(1);
        System.out.println(m == n); //T
        //所以,这里主要是看范围 -128 ~ 127 就是直接返回
        //,否则,就new Integer(xx);
        Integer x = 128;//底层Integer.valueOf(1);
        Integer y = 128;//底层Integer.valueOf(1);
        System.out.println(x == y);//False

    }
}

  

3.

对于示例6和示例7要记住只要有基本数据类型比较的就是值是否相等

public class WrapperExercise03 {
    public static void main(String[] args) {
        //示例一
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//F
//示例二
        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);//F

//示例三
        Integer i5 = 127;//底层Integer.valueOf(127)
        Integer i6 = 127;//-128~127
        System.out.println(i5 == i6); //T
//示例四
        Integer i7 = 128;
        Integer i8 = 128;
        System.out.println(i7 == i8);//F
//示例五
        Integer i9 = 127; //Integer.valueOf(127)
        Integer i10 = new Integer(127);
        System.out.println(i9 == i10);//F

        //示例六
        Integer i11=127;
        int i12=127;
//只有有基本数据类型,判断的是
//值是否相同
        System.out.println(i11==i12); //T
//示例七
        Integer i13=128;
        int i14=128;
        System.out.println(i13==i14);//T


    }
}

四、包装类型和String类型的相互转换

以Integer为例,其他类似

public class WrapperVSString {
    public static void main(String[] args) {
        //Integer -> String
        //方法1
        Integer i = 1100;
        String str1 = i + "";
        //方法2
        String str2 = i.toString();
        String str3 = String.valueOf(i);//进入也是调用toString
        //String -> 包装类(Integer)
        //方法1
        String str4 = "12345";
        Integer i2 = Integer.parseInt(str4);//parseInt返回int然后自动装箱
        //方法2
        Integer integer = new Integer(str4);//构造器可以接一个String

    }
}

五、包装类的常用方法

Integer类和Character类的常用方法

System.out.println(Integer.MIN VALUE); //返回最小值

System.out.println(Integer.MAX VALUE);//返回最大值

System.out.printIn(Character.isDigit("a));//判断是不是数字

System.out.printIn(Character.isLetter("a'));//判断是不是字母

System.out.println(Character.isUpperCase( a ));//判断是不是大写

System.out.println(Character.isLowerCase('a));//判断是不是小写

System.out.println(Character.isWhitespace(' a));//判断是不是空格

System.out.println(Character.toUpperCase( a ));//转成大写

System.out.printIn(Character.toLowerCase('A))//转成小写
 


http://www.kler.cn/news/107655.html

相关文章:

  • TypeScript - 枚举类型 -字符型枚举
  • ETL工具Kettle
  • 深入浅出排序算法之堆排序
  • SQL server 代理服务启动和查看
  • ArcEngine二次开发实用函数16:获取GDB中的所有图层的名称
  • rust 创建多线程web server
  • 子集生成算法:给定一个集合,枚举所有可能的子集
  • 使用docker-compose私有化部署 GitLab
  • 5G与医疗:开启医疗技术的新篇章
  • freeRTOS学习day4-中断使用消息队列
  • 软考系列(系统架构师)- 2012年系统架构师软考案例分析考点
  • Hive常用DDL操作
  • 如何隐藏woocommerce 后台header,woocommerce-layout__header
  • vue中,js获取svg内容并填充到svg图中
  • 信道数据传输速率、信号传播速度——参考《天勤计算机网络》
  • Spring Boot进阶(94):从入门到精通:Spring Boot和Prometheus监控系统的完美结合
  • windows下OOM排查
  • Vite多页面应用简单构建
  • [C++]——带你学习类和对象
  • 电脑报错由于找不到vcruntime140.dll文件怎么修复
  • 在全新ubuntu上用gpu训练paddleocr模型遇到的坑与解决办法
  • python爬虫request和BeautifulSoup使用
  • 在DOS或Windows环境中,使用工具Debug
  • 【斗罗二】霍雨浩迷惑审查,戴华斌故意挑衅,惨败者屈服下跪
  • 金融领域:怎么保持电力系统连续供应?
  • 解决cloudflare pages部署静态页面发生404错误的问题
  • 【AD9361 数字接口CMOS LVDSSPI】B 并行数据之CMOS 续
  • 如何选择最适合 Android 的 SD 卡恢复软件?
  • C++入门精讲——入门看完这一篇就够了
  • rhcsa安装及配置