那些java中的包装类

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

  byte short int long float double char boolean
位数 8 16 32 64 32 64 16 1
字节数 1 2 4 8 4 8 2 1(1/8)
默认值 0 0 0 0L 0.0f 0.0d   false
包装类型 Byte Short Integer Long Float Double Character Boolean

 

 

 

 

 

特别说明:

  • char类型占2个字节,可以表示汉字,所以无论是汉字还是英文字符都占2字节
  • boolean类型理论上占1bit(1/8字节),而实际中按1byte(1字节)处理
  • 基本数据类型之间的转换:低级向高级自动转换(隐式),高级向低级强制转换(显式)
     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); //8
    17         System.out.println(Short.SIZE);//16
    18         System.out.println(Integer.SIZE);//32
    19         System.out.println(Long.SIZE);//64
    20         System.out.println(Float.SIZE);//32
    21         System.out.println(Double.SIZE);//64
    22         System.out.println(Character.SIZE);//16
    23         
    24         /*
    25          * 基本数据类型的默认值
    26          */
    27         DataTypeDemo dtd = new DataTypeDemo();
    28         System.out.println(dtd.a);//0
    29         System.out.println(dtd.b);//0
    30         System.out.println(dtd.c);//0
    31         System.out.println(dtd.d);//0
    32         System.out.println(dtd.e);//0.0
    33         System.out.println(dtd.f);//0.0
    34         System.out.println(dtd.g);//false
    35         System.out.println(dtd.h);//
    36         
    37         /*
    38          * char类型可以表示汉字
    39          */
    40         char ch = '犇';
    41         System.out.println(ch);//
    42     }
    43 
    44 }
 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); //8
17         System.out.println(Short.SIZE);//16
18         System.out.println(Integer.SIZE);//32
19         System.out.println(Long.SIZE);//64
20         System.out.println(Float.SIZE);//32
21         System.out.println(Double.SIZE);//64
22         System.out.println(Character.SIZE);//16
23         
24         /*
25          * 基本数据类型的默认值
26          */
27         DataTypeDemo dtd = new DataTypeDemo();
28         System.out.println(dtd.a);//0
29         System.out.println(dtd.b);//0
30         System.out.println(dtd.c);//0
31         System.out.println(dtd.d);//0
32         System.out.println(dtd.e);//0.0
33         System.out.println(dtd.f);//0.0
34         System.out.println(dtd.g);//false
35         System.out.println(dtd.h);//
36         
37         /*
38          * char类型占2字节,可以表示汉字
39          */
40         char ch = '犇';
41         System.out.println(ch);//
42         
43         /*
44          * 基本数据类型的转换
45          *     低级->高级:自动转换,隐式的
46          *     高级->低级:强制转换,显式的
47          * 另外,常规的算术运算(+、-、*、/)都要求是int型的
48          *     所以对于较int低级的数据类型会进行隐式转换,而较int高级的则要求强转,否则报错
49          */
50         byte n1 = 10, n2 = 20;
51         int n3, n4 = 100;
52         long n5 = 200;
53         //隐式转换
54         n3 = n1;
55         //显式转换
56 //        n3 = n5;    编译报错
57         n3 = (int) n5;
58         
59         //算术运算,"+"两边的操作数会自动转换为int型
60         n3 = n1 + n2;
61 //        n3 = n4 + n5;  编译报错:需要强转
62         //修改后
63         n3 = n4 + (int)n5;
64         n3 = (int) (n4 + n5);
65         
66 //        n3 = n4/(int)n5;
67 //        n3 = (int) (n4/n5);
68     }
69 }

一、包装类的理解

 为了满足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,否则表示false
18          */
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 false
23         
24         /*
25          * 当包装类Number构造方法的参数为String类型时,字符串不能为null
26          *     并且该字符串必须能够解析为基本类型的数据
27          *     否则会抛出数字格式异常。
28          */
29         Integer a2 = new Integer("");//NumberFormatException: For input string: ""
30         Integer a3 = new Integer(null);//NumberFormatException: null
31         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); //100
19         //将字符串按任意进制转换为int数
20         int num2 = Integer.parseInt("100", 8);
21         int num3 = Integer.parseInt("ff", 16);
22         System.out.println(num2); //64
23         System.out.println(num3);
24         
25         //将int数转换为对应的String类型的数
26         String s1 = Integer.toString(100);
27         System.out.println(s1);//100
28         //将int数转换为任意进制的String类型的数,常用于十进制数转换为任意进制数
29         String s2 = Integer.toString(100,8);
30         String s3 = Integer.toString(100,16);
31         System.out.println(s2);//144
32         System.out.println(s3);//64
33     }

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

1         //自动装箱
2         int m = 10;
3         Integer in = m;
4         System.out.println(in);//10
5         
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));//true
11     
12         String s1 = new String("hello");
13         String s2 = new String("hello");
14         System.out.println(s1 == s2);//false
15         System.out.println(s1.equals(s2));//true
16         
17     }

 补充二:java中数组声明的正确语法

总的来说,数组声明的方式有两种:指定数组大小和初始化,并且二者不能同时存在。

 1 public class ArrayDemo {
 2     public static void main(String[] args) {
 3         //指定数组大小
 4         int a[] = new int[3]; 
 5         //数组初始化
 6         int b[] = {1,2,3};    
 7         int[] c = {1,2,3};    
 8         int[] d = new int[]{1,2,3};
 9         
10         /*
11          * 几种错误的语法
12          */
13 //        int a[10] = {};        错误:既指定了大小又想初始化
14 //        int a[3] = {1,2,3};    错误:理由同上
15 //        int[] a = new int[3]{1,2,3};错误:理由同上
16         
17         //必须将声明、创建、初始化都放在一条语句中,否则报错
18 //        int[] arr;
19 //        arr = {1,2,3};
20     }
21 
22 }

 

posted @ 2017-11-14 16:10  Wilange  阅读(17567)  评论(1编辑  收藏  举报