• 包装类


    1.包装类产生的原因:八种基本数据类型并不支持面向对象编程-->不具备"对象"的特性:不携带属性,无法方法可调用

    2.包装类对象封装相应的基本类型的数据,包装类对象一经创建,内容(被封装的基本类型数据值)不可改变

       举个栗子:

     1 public class 包装类 
     2 {
     3   public static void main(String[] args)
     4   {
     5     int num=500;//定义一个普通数据类型
     6     num=200;
     7     Integer obj=new Integer(num);//手动装箱
     8     
     9     System.out.println(obj);
    10         
    11   }
    12 }

     1 public class 包装类 
     2 {
     3   public static void main(String[] args)
     4   {
     5     int num=500;//定义一个普通数据类型
     6     
     7     Integer obj=new Integer(num);//手动装箱
     8     num=200;
     9     System.out.println(obj);
    10         
    11   }
    12 }

     3.基本数据类型-->相应包装类(装箱);包装类-->基本数据类型(拆箱)

     1 public class Pack 
     2 {
     3   public static void main(String[] args)
     4   {
     5     int num=500;//定义一个普通数据类型
     6     Integer obj=new Integer(num);//手动装箱
     7     int n=obj.intValue();//手动拆箱
     8     
     9     System.out.println("num=obj? "+obj.equals(null));
    10     
    11     Integer objr=new Integer(500);
    12     System.out.println("obj等价于objr? "+objr.equals(obj));
    13   }
    14 }

     4.parseInt方法

     1 /*
     2  * shift+alt+z  -->try/catch的快捷键
     3  * 
     4  * parseInt方法,将字符串转换为整数
     5  */
     6 public class Demo_padeInt
     7 {
     8     public static void main(String[] args)
     9     {
    10         String[]str= {"123","9527d","巫妖果子","妖妖灵"};
    11         
    12         for(String str1:str)
    13         {
    14             try {
    15                 int m=Integer.parseInt(str1);//只有包装类才能调用方法
    16                 System.out.println(str1+"可以转换成整数:"+m);
    17                 } 
    18             catch (NumberFormatException e)
    19             {
    20                 System.out.println(str1+" 不可以转换成整数");
    21             }
    22         }
    23     }
    24 }

    5.Integer类的toString方法

     1 /*
     2  * toString方法:将整数转换为字符串
     3  */
     4 public class Test06 
     5 {
     6      public static void main(String[] args) 
     7      {
     8         int n=500;
     9         String m="旋风果子";
    10         String s=Integer.toString(n);//将整数转换为字符串
    11         System.out.println("s"+s);
    12         System.out.println(m+s);//字符串的相连
    13      }
    14 }

    6.自动装箱和自动拆箱

     1 /*
     2  * 自动装箱和自动拆箱
     3  */
     4 public class Test07 
     5 {
     6    public static void main(String[] args)
     7    {
     8     int number=500;
     9     Integer i=number;//自动装箱
    10     int n=number;//自动拆箱功能
    11     System.out.println(i+20);//自动装箱,系统自动调用解析方法i.paseInt()
    12     
    13     Integer obj=500;
    14     System.out.println("i是否等价于obj "+i.equals(obj));
    15    }
    16 }

  • 相关阅读:
    es5中,一个在js中导入另一个js文件。
    移动端字体小于12号字的时候,line-height居中的问题
    初学者都能懂得 Git 说明
    一探 Vue 数据响应式原理
    文件的命名规则
    Vue 的 watch 和 computed 有什么关系和区别?
    MVC 与 Vue
    博客园皮肤设置
    java使用run和start后的线程引用
    Python改变一行代码实现二叉树前序、中序、后序的迭代遍历
  • 原文地址:https://www.cnblogs.com/zjm1999/p/10024618.html
Copyright © 2020-2023  润新知