你的位置:首页 > 软件开发 > Java > 那些java中的包装类

那些java中的包装类

发布时间:2017-11-14 21:00:19
说到Java中的包装类就不得不介绍一下Java中的基本数据类型(8种):byte、short、int、long、float、double、char、boolean。下面以表格的形式详细介绍这八种数据类型: byteshortintlongfloatdoublecharbool ...

说到Java中的包装类就不得不介绍一下Java中的基本数据类型(8种):byte、short、int、long、float、double、char、boolean。下面以表格的形式详细介绍这八种数据类型:

 byteshortintlongfloatdoublecharboolean
位数81632643264161
字节数12484821(1/8)
默认值0000L0.0f0.0d false
包装类型ByteShortIntegerLongFloatDoubleCharacterBoolean

 

 

 

 

 

特别说明:

  • char类型占两个字节,所以可以表示汉字
  • boolean类型理论上占1bit(1/8字节),而实际中按1byte(1字节)处理
那些java中的包装类那些java中的包装类
 1 public class DataTypeDemo { 2  3  byte a; 4  short b; 5  int c; 6  long d; 7  float e; 8  double f; 9  boolean g;10  char h;11  12  public static void main(String[] args) {13   /*14    * 几种基本数据类型的大小(bit)15   */16   System.out.println(Byte.SIZE); //817   System.out.println(Short.SIZE);//1618   System.out.println(Integer.SIZE);//3219   System.out.println(Long.SIZE);//6420   System.out.println(Float.SIZE);//3221   System.out.println(Double.SIZE);//6422   System.out.println(Character.SIZE);//1623   24   /*25    * 基本数据类型的默认值26   */27   DataTypeDemo dtd = new DataTypeDemo();28   System.out.println(dtd.a);//029   System.out.println(dtd.b);//030   System.out.println(dtd.c);//031   System.out.println(dtd.d);//032   System.out.println(dtd.e);//0.033   System.out.println(dtd.f);//0.034   System.out.println(dtd.g);//false35   System.out.println(dtd.h);//36   37   /*38    * char类型可以表示汉字39   */40   char ch = '犇';41   System.out.println(ch);//犇42  }43 44 }
View Code

一、包装类的理解

 为了满足Java语言面向对象的这一特性,上述基本数据类型中的每一个在java.lang包中都有一个包装类,即将每个基本类型都包装成了一个类。常用的包装类可以分为三类:Character、Number、Boolean,具体见上表所示。

这里总结一下包装类的一些特性:

  • 所有包装类都可以将与之对应的基本数据类型作为参数来创建它们的实例对象
  • 除了Character类之外,其他包装类都可以将一个字符串作为参数来构造它们的实例
  • Boolean类的构造方法参数为String类型时,若该字符串为true(不论大小写),则该对象表示true,否则表示false
  • 当包装类Number构造方法的参数为String类型时,字符串不能为null,并且该字符串必须能够解析为基本类型的数据

代码示例:

 1 public static void main(String[] args) { 2   //所有包装类都可以将与之对应的基本数据类型作为参数来创建它们的实例对象 3   Integer a = new Integer(100); 4   Double b = new Double(100.00); 5   Character c = new Character('A'); 6   Boolean d = new Boolean(true); 7   System.out.println(a+" "+ b+" "+c+" "+d);//100 100.0 A true 8    9   //除了Character类之外,其他包装类都可以将一个字符串作为参数来构造它们的实例10   Integer a1 = new Integer("100");11   Double b1 = new Double("100.00");12   Boolean d1 = new Boolean("true");13   System.out.println(a1+" "+ b1+" "+d1);//100 100.0 true  14   15   /*16    * Boolean类的构造方法参数为String类型时:17    *  若该字符串为true(不论大小写),则该对象表示true,否则表示false18   */19   Boolean d2 = new Boolean("True");20   Boolean d3 = new Boolean("TRUE");21   Boolean d4 = new Boolean("hello");22   System.out.println(d2+" "+d3+" "+d4);//true true false23   24   /*25    * 当包装类Number构造方法的参数为String类型时,字符串不能为null26    *  并且该字符串必须能够解析为基本类型的数据27    *  否则会抛出数字格式异常。28   */29   Integer a2 = new Integer("");//NumberFormatException: For input string: ""30   Integer a3 = new Integer(null);//NumberFormatException: null31   Integer a4 = new Integer("abc");//NumberFormatException: For input string: "abc"32   33  }

二、包装类的作用

总的来说,包装类有以下一些用途:

  • 集合不允许存放基本数据类型,故常用包装类
  • 包含了每种基本数据类型的相关属性,如最大值、最小值、所占位数等
  • 作为基本数据类型对应的类类型,提供了一系列实用的对象操作,如类型转换、进制转换等等
 1  private static void getInfo() { 2    3   //获取基本数据类型的相关属性 4   System.out.println(Byte.MIN_VALUE);//-128 5   System.out.println(Byte.MAX_VALUE);//127 6   System.out.println(Byte.SIZE);//8 7    8   /* 9    * 包装类型的一些实用操作,如类型转换、进制转换等10    *  这里以Integer为例,主要介绍intValue、(static)parseInt、(static)toString等方法11   */12   //调用构造器将int类型转换为integer类型,调用intValue方法将integer类型转换为int类型13   Integer in1 = new Integer(100);14   int in2 = in1.intValue();15   16   //将字符串转换为十进制数的int数,常用于任意进制数转换为十进制数17   int num1 = Integer.parseInt("100");18   System.out.println(num1); //10019   //将字符串按任意进制转换为int数20   int num2 = Integer.parseInt("100", 8);21   int num3 = Integer.parseInt("ff", 16);22   System.out.println(num2); //6423   System.out.println(num3);24   25   //将int数转换为对应的String类型的数26   String s1 = Integer.toString(100);27   System.out.println(s1);//10028   //将int数转换为任意进制的String类型的数,常用于十进制数转换为任意进制数29   String s2 = Integer.toString(100,8);30   String s3 = Integer.toString(100,16);31   System.out.println(s2);//14432   System.out.println(s3);//6433  }

事实上,从JDK1.5就开始引入了自动拆装箱的语**能,也就是系统将自动进行基本数据类型和与之相对应的包装类型之间的转换,这使得程序员书写代码更加方便。

1   //自动装箱2   int m = 10;3   Integer in = m;4   System.out.println(in);//105   6   //自动拆箱7   Integer inn = new Integer(10);8   int n = inn;9   System.out.println(n);//10

 

补充:包装类中“==”与equals的用法比较

值得注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址,而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。

 1  private static void equals() { 2   // TODO Auto-generated method stub 3   Integer a = new Integer(-100); 4   Integer b = new Integer("-100"); 5   int c = -100; 6   System.out.println(a == b);//false 7   System.out.println(a == c);//true 8   System.out.println(b == c);//true 9   10   System.out.println(a.equals(b));//true11  12   String s1 = new String("hello");13   String s2 = new String("hello");14   System.out.println(s1 == s2);//false15   System.out.println(s1.equals(s2));//true16   17  }

 

原标题:那些java中的包装类

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。