zoukankan      html  css  js  c++  java
  • 包装类

    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 }

  • 相关阅读:
    【PyMuPDF和pdf2image】Python将PDF转成图片PNG和JPG
    How to extract images from PDF in Python? 通过python从pdf文件中提取图片
    linux shell 用sed命令在文本的行尾或行首添加字符
    shell中的if语句
    Linux find 用法示例
    Linux 自动删除N小时或分钟前的文件
    记一次Linux磁盘空间占满无法删除的故障
    Shell字符串拼接(连接、合并)
    shell脚本使用之awk按列求平均值(含最大,最小值)
    LVM实现将2块磁盘总空间“合二为一”并挂载到同一目录
  • 原文地址:https://www.cnblogs.com/zjm1999/p/10024618.html
Copyright © 2011-2022 走看看