java学习日记20230403-包装类

包装类

  • 针对八种数据类型相应的引用类型;
  • 有了类的特点,就可以调用类的方法
  • boolean Boolean
    char

    Char

    byte Byte
    short Short
    long Long
    int Integer
    float Float
    double Double
  • 包装类和基本数据类型的转换
    • jdk5前手动拆箱和装箱,装箱:基本类型-包装类型
    • jdk5后,自动装箱和拆箱
    • 自动装箱底层调用的是valueof方法
    • 查看代码
      
      public class Wrapper01 {
          public static void main(String[] args) {
      
              int n1 =3;
              //自动装箱 Integer.ValueOf(n1);
              Integer integer = n1;
              //自动拆箱 底层使用是intValue();
              int n2 = integer;
              System.out.println(integer.compareTo(n1));
          }
      }
    • 三元运算符要看为一个整体
    • true?new Integer(1):new Double(2.0). -----返回1.0 而不是1
  • 包装类和String互相转化
    •   查看代码
      public class WrapperToString {
          public static void main(String[] args) {
              Integer integer = 100;
              //方式1
              String string1 = integer +"";
              //方式2
              String s = integer.toString();
              //方式3
              String s1 = String.valueOf(integer);
      
              //String - 包装类
              String str = "123";
              Integer i = Integer.parseInt(str);
              Integer integer1 = new Integer(str);
          }
      }

      创建Interger类型的对象时,若数字在-128-127范围内,则变量直接指向常量池,若超出范围,则在堆内存中new一个对象   
      只要有基本数据类型,==判断的是值相等

posted @   、子夜  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示