• 包装类、及装箱和拆箱


    包装类是一种特殊的类,在java 的lang包中。用来将八种基本数据类型包装为对象。

    以下是基本数据类型与包装类的对应关系:

    byte Byte;short Short;int Integer;long Long;float Float;double Double;char Charactor;boolean Boolean。

    各种包装类的构造器和适用方法都很类似,下面以最常用的Integer包装类做示例。

    构造器:

    Integer(int i): 将int类型数据i包装成Integer类对象

    Integer(String s): 将字符串s包装成Integer类对象。注意:s必须是整数字符串类型。

    常用方法:

    Static int    parseInt(String s):类方法,将字符串转换为整型数据

    eg:int i=Integer.parseInt("15")

    int  intValue():计算包装类对象的值

    eg:Integer a=new Integer("12");

          int i=a.intValue()

    static String     toHexString(int i):类方法,将一个整数转换为16进制字符串

     eg:

         String str=Integer.toHexString(17)

    String   toString():将包装类对象转换为字符串

    eg:

        Integer b=12;//装箱

       String str=b.toString();

    static Integer  valueOf(int i):类方法,将整数转化为包装类对象

    eg: Integer a=Integer.valueOf(7);

    装箱:

    Integer a=12;//装箱是快捷将int数据转化为包装类对象

    拆箱

    Integer b=9;

    int a=b;//拆箱将包装类对象转换为整数。

  • 相关阅读:
    Java图像处理
    Java图像处理
    Java文字图像识别(1)[88250原创]
    简单的Java图像处理程序
    英语、日语
    My First English Thread
    word的常用操作
    C++中结构体的的慨念和使用方法
    C#调用dll(C++(Win32))时的类型转换总结
    设置VS2008和IE8 调试ATL MFC ActiveX控件
  • 原文地址:https://www.cnblogs.com/hitnmg/p/9346638.html
Copyright © 2020-2023  润新知