Java_包装类

3.3、包装类(重点)

         在Java的设计之中,一直提倡一个原则:一切皆对象,这个原则本身有一个漏洞 —— 基本数据类型不是对象,所以这个原则就出现了问题。那么如果说现在这个问题由我们来解决,该如何做呢?

class Int {   // 类

         private int num ;  // 基本型

         public Int(int num) {

                   this.num = num ;

         }

         public int intValue() {

                   return this.num ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   Int temp = new Int(10) ;         // 把基本类型变为类

                   int result = temp.intValue() ;

                   System.out.println(result * result) ;

         }

}

         以上的操作是将基本类型变为了一个对象的形式进行操作了,但是这里面有一个问题:基本数值型数据是可以进行数学运算的,可是以上变为了类的形式,那么肯定无法直接计算了。而且以上的问题既然我们都可以想到方法去解决,那么java也一定早已解决,为此它专门提供了八种包装类:byteByte)、shortShort)、intInteger)、longLong)、floatFloat)、doubleDouble)、booleanBoolean)、charCharacter,而这八种包装类又分为两大阵营:

                   · 数值型(Number子类):Byte、Short、Integer、Float、Double、Long;

                   · 对象型(Object子类):Boolean、Character。

         可是对于Number的子类,就必须观察出Number类之中定义的方法:byteValue()、intValue()、doubleValue()、shortValue()、floatValue()、doubleValue(),就是从包装的类之中取得所包装的数值。

3.3.1 、装箱与拆箱

         在基本数据类型和包装类之间的转换操作之中分为两个重要概念:

                   · 装箱操作:将基本数据类型变为包装类,称为装箱,包装类的构造方法;

                   · 拆箱操作:将包装类变为基本数据类型,称为拆箱,各个类的xxValue()方法。

范例:以int和Integer为例

public class TestDemo {

         public static void main(String args[]) {

                   Integer var = new Integer(15) ;       // 装箱

                   int result = var.intValue() ;     // 拆箱

                   System.out.println(result * result) ;

         }

}

范例:以double和Double为例

public class TestDemo {

         public static void main(String args[]) {

                   Double var = new Double(15.5) ;   // 装箱

                   double result = var.doubleValue() ; // 拆箱

                   System.out.println(result * result) ;

         }

}

范例:以boolean和Boolean为例

public class TestDemo {

         public static void main(String args[]) {

                   Boolean var = new Boolean(true) ; // 装箱

                   boolean result = var.booleanValue() ;       // 拆箱

                   if (result) {

                            System.out.println("Hello World .") ;

                   }

         }

}

         以上的操作是在JDK 1.5之前所进行的必须的操作,但是到了JDK 1.5之后,Java提供了自动的装箱和拆箱机制,并且包装类的对象可以自动的进行数学计算了。

public class TestDemo {

         public static void main(String args[]) {

                   Integer var = 15 ; // 自动装箱

                   int result = var ;   // 自动拆箱

                   System.out.println(++ var * result) ;

         }

}

public class TestDemo {

         public static void main(String args[]) {

                   Boolean var = true ;      // 自动装箱

                   if (var) {     // 自动拆箱后操作

                            System.out.println("Hello World .") ;

                   }

         }

}

         那么正是因为有了这样的自动装箱和拆箱的机制,所以Object也可以接收基本数据类型的数据。

public class TestDemo {

         public static void main(String args[]) {

                   Object obj = 15 ; // int --> 自动装箱 --> Object

                   int result = (Integer) obj ; // Object --> 包装类 --> 自动拆箱

                   System.out.println(result * result) ;

         }

}

         但是到此处还有一个小问题,实际上这一问题之前已经见过的。

public class TestDemo {

         public static void main(String args[]) {

                   Integer x = new Integer(10) ; // 新空间

                   Integer y = 10 ;    // 入池

                   Integer z = 10 ;

                   System.out.println(x == y) ;  // false

                   System.out.println(x == z) ;   // false

                   System.out.println(z == y) ;   // true

                   System.out.println(x.equals(y)) ;     // true

         }

}

         以后使用包装类的时候还需要考虑equals()和==的区别。

3.3.2 、数据转型

         包装类之中所提供的最大优点还在于可以将字符串变为指定的基本数据类型,下面列出几个操作:

                   · Integer类:public static int parseInt(String s);

                   · Double类:public static double parseDouble(String s);

                   · Boolean类:public static boolean parseBoolean(String s);

         但是Character这个包装类之中,并没有提供一个类似的parseCharacter(),因为字符串String类之中提供了一个charAt()方法,可以取得指定索引的字符,而且一个字符的长度就是一位。

范例:将字符串变为int

public class TestDemo {

         public static void main(String args[]) {

                   String str = "16" ;

                   int result = Integer.parseInt(str) ;     // String --> int

                   System.out.println(result * result) ;

         }

}

         但是需要提醒的是,在执行这种转换的操作过程之中,字符串中的全部内容必须由数字所组成,如果有一位内容不是数字,则在转换的过程之中将出现如下的错误提示:NumberFormatException。

范例:将字符串变为double

public class TestDemo {

         public static void main(String args[]) {

                   String str = "36." ;

                   double result = Double.parseDouble(str) ;

                   System.out.println(result * result) ;

         }

}

范例:将字符串变为boolean型数据

public class TestDemo {

         public static void main(String args[]) {

                   String str = "true" ;

                   boolean result = Boolean.parseBoolean(str) ;

                   if (result) {

                            System.out.println("Hello World .") ;

                   }

         }

}

         提示:在使用Boolean型包装类的时候,如果字符串之中的内容不是true或者是false,统一都按照false处理。

         以上的操作是通过字符串变为了一些基本类型的数据,但是反过来讲,基本类型如何变为字符串呢?

                   · 方式一:任何的基本类型数据遇到了String之后都变为String型数据;

public class TestDemo {

         public static void main(String args[]) {

                   int num = 100 ;

                   String str = num + "" ;          // int --> String

                   System.out.println(str.length()) ;

         }

}

                   · 方式二:利用String类的方法,public static String valueOf(数据类型 b)

public class TestDemo {

         public static void main(String args[]) {

                   int num = 100 ;

                   String str = String.valueOf(num) ;       // int --> String

                   System.out.println(str.length()) ;

         }

}

         字符串向各个基本数据类型的转换,是一个非常重要的内容,必须熟练。

posted @ 2013-04-12 17:25  谷文仁  阅读(199)  评论(0编辑  收藏  举报