码迷,mamicode.com
首页 > 其他好文 > 详细

包装类

时间:2016-03-05 11:45:32      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

/*包装类*/ /*  byte   Byte  short   Short  int    Integer  long   Long  char   Character  float   Float  double   Double  boolean   Boolean

 基本数据和其包装类:作为成员变量时的默认初始值的差异:  int  -- Integer  0        null   */ class WrapDemo {  public static void main(String[] args)  { 

  //包装类对象   /*    把基本数据类型包装类除了 Character类之外,其他的7个都有两个构造方法

    基本数据类型xx;  xx对应包装类的构造方法:         public  xx的包装类(xx x){}         public  xx的包装类(String x){}

        这里传递的x值表示该包装类型对象的值;         Integer i = new Integer(17);//i  = 17         i  = new Integer("123");// i = 123                  使用第二种构造方法可能会引发 类型转换异常;                 Integer:凡是涉及到整数的操作,Integer类里都有相应的方法来处理;

 

    Character类没有public  Character(String c){}

  */       Boolean b = new Boolean("tRuE");//除了true之外(不区分大小写),其他的默认都是false

  /*    static Boolean FALSE       对应基值 false 的 Boolean 对象。    static Boolean TRUE          对应基值 true 的 Boolean 对象。

  */   b = Boolean.FALSE;//FALSE == new  Boolean(false);   System.out.println("b= " + b);

  //十进制和  二进制,八进制,十六进制的转换

  /*   Integer:    有3个静态的方法可以完成:     static String toBinaryString(int i)      以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。     static String toHexString(int i)      以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。     static String toOctalString(int i)      以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。     */   System.out.println(Integer.toBinaryString(-100));      //byte byteValue() 以 byte 类型返回该 Integer 的值。      Integer i  = new Integer(17);   byte by   =  i.byteValue();   System.out.println(by);      /*     int compareTo(Integer anotherInteger)在数字上比较两个 Integer 对象。   */

  System.out.println(i.compareTo(new Integer(12)));//1   /*     int intValue()  以 int 类型返回该 Integer 的值。

    包装类 -->基本类型:      调用xxxValue(); int ret  = age.intValue();

    基本类型 -->其包装类

    new  包装类(基本类型的值); new Integer(17);   */

  int age = i.intValue();

  /*    static int parseInt(String s)  将字符串参数作为有符号的十进制整数进行解析。

   String  --> 基本类型   */      age = Integer.parseInt("17");   /*    static Integer valueOf(int i)   基本类型 -->包装类类型 和 new Integer(int i);       返回一个表示指定的 int 值的 Integer 实例。    static Integer valueOf(String s)  String -->  Integer: new  Integer(String s);       返回保存指定的 String 的值的 Integer 对象。

  */   //static boolean isDigit(char ch) 确定指定字符是否为数字。 //判断的是0  ~9之间的数字   System.out.println(Character.isDigit(‘A‘));//false      char c = ‘8‘;//   System.out.println(Character.isDigit(c));

  String s = "1214542S";      System.out.println("----------------");   int i1 = 12;   int i2 = 12;

  System.out.println(i1 == i2);//相等

  Integer in1  = new Integer(12);   Integer in2  = new Integer(12);

  System.out.println(in1 == in2);//不相等的,两个对象

  in1 = 12;   in2 = 12;   System.out.println("--->"+(in1 == in2));//true      /*    这里涉及到了一个享元模式: 只缓存[-128,127]区间的数字,Byte,Short,Integer,Long 都是一样的   */   in1 = 128;   in2 = 128;   System.out.println("--->"+(in1 == in2));//true

  /*    装箱和拆箱:    

   int  Integer    装箱: 把基本数据类型 --> 它所对应的包装类类型      Integer i = new Integer(int value);

   拆箱: 包装类类型   --> 它所对应的把基本数据类型       int i2  = i.intValue();

   Java5开始提供了自动装箱和拆箱的功能;    自动装箱:  可以把一个基本数据类型的值直接赋给 其包装类对象       Integer age = 17;

    Object就可以接受一切类型了;  Object o = false;  o = 123;         自动拆箱:  可以把一个包装类对象直接赋给一个基本数据类型的变量       int age2  = age;       

  */

  Integer age2 =  17;

  int age3 = age2;

  

 } }

包装类

标签:

原文地址:http://www.cnblogs.com/hoobey/p/5244356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!