一起Talk Android吧(第二十六回:Java包装类)
2017-06-02 07:08
239 查看
各位看官们,大家好,上一回中咱们说的是Java多线程编程的例子,这一回咱们说的例子是Java包装类。闲话休提, 言归正转。让我们一起Talk Android吧!
看官们,大家都知道Java是面向对象的语言,因此有着一切兼对象的思想,不过Java中的基本数据类型不符合这种思想,为此,Java提供了包装类。它把基本的数据类型包装成了类类型。包装类的名称由此而来。
包装类的名称很容易识别,把基本类型的首字母大写后就是包装类,比如
包装类都属于类类型,因此创建包装类对象的方法和创建普通类对象的方法完全相同,使用new实例化一个对象就可以。不过我们在实际使用过程中,常用的做法是把基本类型的变量转换为包装类,该过程称为装箱;或者把包装类转换为基本类型,该过程称为折箱。
装箱和折箱都是使用包装类提供的方法来完成的,只是使用的方法不同而已。装箱时使用的是包装类的构造方法,折箱时使用的是包装类的
这时有看官说这样换来换去的,还需要借助其它方法才能完成装箱和折箱,有没有更加方便的办法呢?有。我们除了使用包装类的方法手动进行装箱和折箱外,还可以进行自动装箱或者折箱。此时使用等号就可以实现自动装箱或者折箱。
包装类还有一个常用的功能是把字符串类型的变量转换为基本类型的变量,我们使用包装类的
说了这么多概念上的东西,估计大家都感觉厌烦了,接下来我们通过具体的代码来演示:
看官们,上面是完成的代码,大家可以结合刚才介绍的概念来阅读代码,如果有不明白的地方,可以参考代码中的注释。
下面是程序的运行结果,请大家参考:
看官们,通过今天的介绍,我相信大家已经学会了包装类,并且掌握了如何装箱和折箱,不过,在使装箱和折箱的过程中,会有一定的性能损失,这一点希望大家能够明白。
各位看官,关于Java包装类的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!
看官们,大家都知道Java是面向对象的语言,因此有着一切兼对象的思想,不过Java中的基本数据类型不符合这种思想,为此,Java提供了包装类。它把基本的数据类型包装成了类类型。包装类的名称由此而来。
包装类的名称很容易识别,把基本类型的首字母大写后就是包装类,比如
byte-Byte,long-Long。前者是基本类型,后者是包装类型。不过也有两个特殊的类型不符合这个规则,它们是:
int-Integer,char-Character。
包装类都属于类类型,因此创建包装类对象的方法和创建普通类对象的方法完全相同,使用new实例化一个对象就可以。不过我们在实际使用过程中,常用的做法是把基本类型的变量转换为包装类,该过程称为装箱;或者把包装类转换为基本类型,该过程称为折箱。
装箱和折箱都是使用包装类提供的方法来完成的,只是使用的方法不同而已。装箱时使用的是包装类的构造方法,折箱时使用的是包装类的
xxxValue()方法,这个xxx表示
int,double等基本类型。
这时有看官说这样换来换去的,还需要借助其它方法才能完成装箱和折箱,有没有更加方便的办法呢?有。我们除了使用包装类的方法手动进行装箱和折箱外,还可以进行自动装箱或者折箱。此时使用等号就可以实现自动装箱或者折箱。
包装类还有一个常用的功能是把字符串类型的变量转换为基本类型的变量,我们使用包装类的
parseXXX方法就能实现该功能,这里的XXX表示
int,double等基本数据类型。
说了这么多概念上的东西,估计大家都感觉厌烦了,接下来我们通过具体的代码来演示:
public class PkgClassEx { public static void main(String[] args) { int basicIntValue1 = 6; int basicIntValue2; int basicIntValue3; char basicCharValue1 = 'a'; char basicCharValue2; char basicCharValue3; // code of boxing Integer pkgIntValue = new Integer(basicIntValue1); Character pkgCharValue = new Character(basicCharValue1); System.out.println("the value of Basic type is: "+basicIntValue1+" "+basicCharValue1); System.out.println("the value of package type is: "+pkgIntValue+" "+pkgCharValue); // code of unboxing basicIntValue2 = pkgIntValue.intValue(); basicCharValue2 = pkgCharValue.charValue(); System.out.println("the value of Basic type is: "+basicIntValue2+" "+basicCharValue2); //code of auto-unboxing basicIntValue3 = pkgIntValue; basicCharValue3 = pkgCharValue; System.out.println("the value of Basic type is: "+basicIntValue3+" "+basicCharValue3); //code of change the type of value from String to basic type System.out.println("change the type of value from String to basic type"); System.out.println("From "+ "6" + " to "+Integer.parseInt("6")); System.out.println("From "+ "6.0" + " to "+Float.parseFloat("6.0")); } }
看官们,上面是完成的代码,大家可以结合刚才介绍的概念来阅读代码,如果有不明白的地方,可以参考代码中的注释。
下面是程序的运行结果,请大家参考:
the value of Basic type is: 6 a the value of package type is: 6 a the value of Basic type is: 6 a the value of Basic type is: 6 a change the type of value from String to basic type From 6 to 6 From 6.0 to 6.0
看官们,通过今天的介绍,我相信大家已经学会了包装类,并且掌握了如何装箱和折箱,不过,在使装箱和折箱的过程中,会有一定的性能损失,这一点希望大家能够明白。
各位看官,关于Java包装类的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!
相关文章推荐
- 一起Talk Android吧(第二十三回:Java多线程编程一)
- 一起Talk Android吧(第五回:Java中的程序结构)
- 一起Talk Android吧(第十一回:Java中的继承)
- 一起Talk Android吧(第七回:Java综合小练习)
- 一起Talk Android吧(第九回:Java中的封装:一)
- 一起Talk Android吧(第三回:Java中的变量类型)
- 一起Talk Android吧(第十二回:Java中的多态)
- 一起Talk Android吧(第十三回:Java面向对象综合练习)
- 一起Talk Android吧(第十七回:Java常用类之StringBuffer)
- 一起Talk Android吧(第十六回:Java常用类之String)
- 一起Talk Android吧(第二回:Java版Hello World)
- 一起Talk Android吧(第十九回:Java常用类之Date)
- 一起Talk Android吧(第二十五回:Java多线程编程三)
- 一起Talk Android吧(第六回:Java中的函数)
- 一起Talk Android吧(第二十回:Java常用类之Date续)
- 一起Talk Android吧(第八回:Java中的面向对象)
- 一起Talk Android吧(第二十一回:Java常用类之文件操作一)
- 一起Talk Android吧(第十八回:Java常用类String VS StringBuffer)
- 一起Talk Android吧(第十五回:Java常用类之Arrays)
- 一起Talk Android吧(第十四回:Java中的异常)