java学习之路---包装类

来源:互联网 发布:大数据4v 问题 编辑:程序博客网 时间:2024/06/06 10:26
万物接对象,那么基本数据也是对象吗?
不是,那么怎么变成对象喃?(经过包装就OK了)
看下面的表


Integer ,Byte,Float,Long,Double,Short都是属于Number类的子类

Character, Boolean 都是object的子类


2.拆箱与装箱

装箱就是:把基本数据转为包装类。
拆箱:包装类变为基本数据
现在只需要了解就OK了,值得是怎么回事,java现在把拆箱和装箱都自动化了

3.包装的应用
就是类型之间的互相转换

  1. /** 
  2.  * <p>TypeChange.java 
  3.  * <p>JAVA基本类型互相转换 
  4.  *  
  5.  * @author heardy 
  6.  * @time 2011/01/30 
  7.  * @version 1.0 
  8.  */  
  9. public class TypeChange {  
  10.   
  11.     /** 
  12.      * 转为字符串 
  13.      *  
  14.      * @param obj 
  15.      * @return 为null时返回空字符串 
  16.      */  
  17.     public static String objectToString(Object obj) {  
  18.     String str = "";  
  19.     try {  
  20.         str = (String) obj;  
  21.         if (str == null) {  
  22.         str = "";  
  23.         }  
  24.     } catch (ClassCastException ce) {  
  25.         try {  
  26.         str = String.valueOf(obj);  
  27.         } catch (Exception e) {  
  28.         str = "";  
  29.         }  
  30.     }  
  31.     return str.trim();  
  32.     }  
  33.   
  34.     /** 
  35.      * 转null为空字符 
  36.      *  
  37.      * @param str 
  38.      * @return 
  39.      */  
  40.     public static String nullOfString(Object obj) {  
  41.     String str = objectToString(obj);  
  42.     return str;  
  43.     }  
  44.   
  45.     /** 
  46.      * 转null为Int 
  47.      *  
  48.      * @param obj 
  49.      * @return 
  50.      */  
  51.     public static int nullOfInt(Object obj) {  
  52.     return objectToInt(obj);  
  53.     }  
  54.   
  55.     // *****************Object 转 其他类型*****************  
  56.   
  57.     /** 
  58.      * 转字节 
  59.      *  
  60.      * @param str 
  61.      * @return 
  62.      * @throws 如果无法转换 
  63.      *             抛出 NumberFormatException 
  64.      */  
  65.     public static byte objectToByte(Object obj) {  
  66.     String str = objectToString(obj);  
  67.     str = nullOfString(str);  
  68.     try {  
  69.         return Byte.parseByte(str);  
  70.     } catch (NumberFormatException e) {  
  71.         System.out.println("转换出错" + e);  
  72.         throw e;  
  73.     }  
  74.     }  
  75.   
  76.     /** 
  77.      * 转布尔 
  78.      *  
  79.      * @param str 
  80.      * @return 为1或"true"返回真 否则假 
  81.      */  
  82.     public static boolean objectToBoolean(Object obj) {  
  83.     String str = objectToString(obj);  
  84.     if (str.equals("1")) {  
  85.         return true;  
  86.     } else if (str.equals("0")) {  
  87.         return false;  
  88.     } else {  
  89.         try {  
  90.         return Boolean.parseBoolean(str);  
  91.         } catch (Exception e) {  
  92.         return false;  
  93.         }  
  94.     }  
  95.     }  
  96.   
  97.     /** 
  98.      * 转Int 
  99.      *  
  100.      * @param str 
  101.      * @return 相应的值 或 零 
  102.      */  
  103.     public static int objectToInt(Object obj) {  
  104.     String str = objectToString(obj);  
  105.     int i = 0;  
  106.     try {  
  107.         i = Integer.parseInt(str.trim());  
  108.     } catch (NumberFormatException e) {  
  109.         i = 0;  
  110.     }  
  111.     return i;  
  112.     }  
  113.   
  114.     /** 
  115.      * 转short 
  116.      *  
  117.      * @param obj 
  118.      * @return 相应的值 或 零 
  119.      */  
  120.     public static short objectToShort(Object obj) {  
  121.     String str = objectToString(obj);  
  122.     short i = 0;  
  123.     try {  
  124.         i = Short.parseShort(str.trim());  
  125.     } catch (NumberFormatException e) {  
  126.         i = 0;  
  127.     }  
  128.     return i;  
  129.     }  
  130.   
  131.     /** 
  132.      * 转Double 
  133.      *  
  134.      * @param obj 
  135.      * @return 相应的值 或 零 
  136.      */  
  137.     public static double objectToDouble(Object obj) {  
  138.     String str = objectToString(obj);  
  139.     double i = 0;  
  140.     try {  
  141.         i = Double.parseDouble(str.trim());  
  142.     } catch (NumberFormatException e) {  
  143.         i = 0;  
  144.     }  
  145.     return i;  
  146.     }  
  147.   
  148.     /** 
  149.      * 转Long 
  150.      *  
  151.      * @param obj 
  152.      * @return 相应的值 或 零 
  153.      */  
  154.     public static long objectToLong(Object obj) {  
  155.     String str = objectToString(obj);  
  156.     Long li = new Long(0);  
  157.     try {  
  158.         li = Long.valueOf(str);  
  159.     } catch (NumberFormatException e) {  
  160.     }  
  161.     return li.longValue();  
  162.     }  
  163.   
  164.     // ***************** 其他类型相互转换 *****************  
  165.   
  166.     /** 
  167.      * double转long 
  168.      *  
  169.      * @param d 
  170.      * @return 只截取前面的整数 
  171.      */  
  172.     public static long doubleToLong(double d) {  
  173.     long l = 0;  
  174.     try {  
  175.         // double转换成long前要过滤掉double类型小数点后数据  
  176.         l = Long.parseLong(String.valueOf(d).substring(0,  
  177.             String.valueOf(d).lastIndexOf(".")));  
  178.     } catch (Exception e) {  
  179.         l = 0;  
  180.     }  
  181.     return l;  
  182.     }  
  183.   
  184.     /** 
  185.      * double转int 
  186.      *  
  187.      * @param d 
  188.      * @return 只截取前面的整数 
  189.      */  
  190.     public static int doubleToInt(double d) {  
  191.     int i = 0;  
  192.     try {  
  193.         // double转换成Int前要过滤掉double类型小数点后数据  
  194.         i = Integer.parseInt(String.valueOf(d).substring(0,  
  195.             String.valueOf(d).lastIndexOf(".")));  
  196.     } catch (Exception e) {  
  197.         i = 0;  
  198.     }  
  199.     return i;  
  200.     }  
  201.   
  202.     /** 
  203.      * double转long(四舍五入) 
  204.      *  
  205.      * @param d 
  206.      * @return 只截取前面的整数 (四舍五入) 
  207.      */  
  208.     public static long doubleToLongWhithRound(double d) {  
  209.     long l = 0;  
  210.     try {  
  211.         l = Math.round(d);  
  212.     } catch (Exception e) {  
  213.         l = 0;  
  214.     }  
  215.     return l;  
  216.     }  
  217.   
  218.     /** 
  219.      * double转int(四舍五入) 
  220.      *  
  221.      * @param d 
  222.      * @return 只截取前面的整数 (四舍五入) 
  223.      */  
  224.     public static int doubleToIntWhithRound(double d) {  
  225.     int i = 0;  
  226.     try {  
  227.         i = (int) Math.round(d);  
  228.     } catch (Exception e) {  
  229.         i = 0;  
  230.     }  
  231.     return i;  
  232.     }  
  233.   
  234.     public static double longToDouble(long d) {  
  235.     double l = 0;  
  236.     try {  
  237.         l = Double.parseDouble(String.valueOf(d));  
  238.     } catch (Exception e) {  
  239.         l = 0;  
  240.     }  
  241.     return l;  
  242.     }  
  243.   
  244.     public static int longToInt(long d) {  
  245.     int l = 0;  
  246.     try {  
  247.         l = Integer.parseInt(String.valueOf(d));  
  248.     } catch (Exception e) {  
  249.         l = 0;  
  250.     }  
  251.     return l;  
  252.     }  


原创粉丝点击