Java - 封装类

来源:互联网 发布:淘宝天猫开店费用 编辑:程序博客网 时间:2024/06/05 23:50

基本数据类型和封装类对照表

[plain] view plain copy
  1. 基本数据类型  封装类    
  2. byte        Byte    
  3. short       Short    
  4. int         Integer    
  5. long        Long    
  6. float       Float    
  7. double      Double    
  8. char        Character    
  9. boolean     Boolean  

基本概念

[plain] view plain copy
  1. 封装类功能  
  2.     将基本数据类型封装当对象操作  
  3.     为基本数据类型提供各种转换功能  
  4. 封装类一旦被创建,内容永不改变,如需封装新的内容,创建新对象  
  5.     通过修改封装类引用的指向来实现如同修改对象值  
  6. 封装类都是final的类,不能被继承  

Integer

[plain] view plain copy
  1. final  
  2. extends Number  
  3. implements Comparable<Integer>  

Integer常用方法

[java] view plain copy
  1. package com.itlwc;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.         // int转型为Integer  
  6.         new Integer(10);  
  7.         Integer.valueOf(10);  
  8.   
  9.         // int转型为String  
  10.         Integer.toString(10);  
  11.         // int转型为String带进制  
  12.         Integer.toString(102);  
  13.         Integer.toString(108);  
  14.         Integer.toString(1016);  
  15.         Integer.toBinaryString(10);  
  16.         Integer.toOctalString(10);  
  17.         Integer.toHexString(10);  
  18.   
  19.         // String转型为Integer  
  20.         new Integer("10");  
  21.         Integer.valueOf("10");  
  22.         Integer.decode("10");  
  23.         // String转型为Integer带进制  
  24.         Integer.valueOf("10"2);  
  25.         Integer.valueOf("10"8);  
  26.         Integer.valueOf("10"16);  
  27.         // String转型为int  
  28.         Integer.parseInt("10");  
  29.         // String转型为int带进制  
  30.         Integer.parseInt("10"2);  
  31.         Integer.parseInt("10"8);  
  32.         Integer.parseInt("10"16);  
  33.   
  34.         // 返回符号 1为正 -1为负 0为0  
  35.         Integer.signum(10);  
  36.         // 返回byte  
  37.         new Integer(10).byteValue();  
  38.         // 返回short  
  39.         new Integer(10).shortValue();  
  40.         // 返回int  
  41.         new Integer(10).intValue();  
  42.         // 返回long  
  43.         new Integer(10).longValue();  
  44.         // 返回double  
  45.         new Integer(10).doubleValue();  
  46.         // 返回float  
  47.         new Integer(10).floatValue();  
  48.         // 比较此对象与指定对象  
  49.         new Integer(10).equals(10);  
  50.         // 在数字上比较两个 Integer 对象  
  51.         new Integer(10).compareTo(10);  
  52.   
  53.         // 最大值最小值  
  54.         int max = Integer.MAX_VALUE;  
  55.         int min = Integer.MIN_VALUE;  
  56.   
  57.     }  
  58. }  

Integer的缓存机制

[java] view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Integer a = 100;  
  4.         /* 
  5.          * 如果使用这种方法为i赋值 
  6.          * Integer类有一个缓存机制 
  7.          * Integer类认为认为-128~127之间的整数是我们经常用到的数值 
  8.          * 因为100在这个范围就不生成对象,使用缓存好的对象 
  9.          */  
  10.         Integer b = 100;  
  11.         if (a == b) {  
  12.             System.out.println("a==b");  
  13.         } else {  
  14.             System.out.println("a!=b");  
  15.         }  
  16.         Integer aa = 200;  
  17.         // 因为200不在这个范围生成新的对象  
  18.         Integer bb = 200;  
  19.         if (aa == bb) {  
  20.             System.out.println("aa==bb");  
  21.         } else {  
  22.             System.out.println("aa!=bb");  
  23.         }  
  24.         // 如果使用构造方法就没有缓存  
  25.         Integer aaa = new Integer(100);  
  26.         Integer bbb = new Integer(100);  
  27.         if (aaa == bbb) {  
  28.             System.out.println("aaa==bbb");  
  29.         } else {  
  30.             System.out.println("aaa!=bbb");  
  31.         }  
  32.     }  
  33. }  
  34. 打印:  
  35.     a==b  
  36.     aa!=bb  
  37.     aaa!=bbb  

valueOf() VS parseXxx()

[plain] view plain copy
  1. valueOf()可以以基本数据类型和字符串为参数,返回的是封装类对象的引用  
  2. parseXxx()只能以字符串为参数,返回的是基本数据类型的值  

isNaN()

[plain] view plain copy
  1. 判断Double,Float对象封装的值是否是NaN  
  2. 只有浮点数对应的封装类具有该方法  
  3. 该方法被重载了  
  4.     一个无参数的,非静态的,boolean isNaN()  
  5.     一个是有参数的,静态的,static boolean isNaN(x)  
  6.         x代表float或者double  

案例

[java] view plain copy
  1. package com.itlwc;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.         Double d = new Double(12.0%0);  
  6.         if(d.isNaN()){  
  7.             System.out.println("封装的对象值为NaN");  
  8.         }else{  
  9.             System.out.println("封装的对象值为"+d);  
  10.         }  
  11.     }  
  12. }  
  13. /* 
  14. 打印结果: 
  15.     封装的对象值为NaN 
  16. */  

java.math.BigInteger

[java] view plain copy
  1. package com.itlwc;  
  2.   
  3. import java.math.BigInteger;  
  4.   
  5. public class Test {  
  6.     public static void main(String[] args) {  
  7.         BigInteger bi1 = new BigInteger("222222222222222222222");  
  8.         BigInteger bi2 = new BigInteger("111111111111111111111");  
  9.         System.out.println("bi1+bi2: "+ bi1.add(bi2));  
  10.         System.out.println("bi1-bi2: "+ bi1.subtract(bi2));  
  11.         System.out.println("bi1*bi2: "+ bi1.multiply(bi2));  
  12.         System.out.println("bi1/bi2: "+ bi1.divide(bi2));  
  13.         System.out.println("bi1%bi2: "+ bi1.mod(bi2));  
  14.         System.out.println("bi1的负数: "+ bi1.negate());  
  15.         //取bi1的符号  
  16.         if(bi1.signum()==1){  
  17.             System.out.println("bi1为整数");  
  18.         }else{  
  19.             System.out.println("bi1为负数");  
  20.         }  
  21.         //bi1与bi2的大小  
  22.         if(bi1.compareTo(bi2)>0){  
  23.             System.out.println("bi1大于bi2");  
  24.         }else{  
  25.             System.out.println("bi1小于bi2");  
  26.         }  
  27.     }  
  28. }  
  29. /* 
  30. 打印结果: 
  31.     bi1+bi2: 333333333333333333333 
  32.     bi1-bi2: 111111111111111111111 
  33.     bi1*bi2: 24691358024691358024641975308641975308642 
  34.     bi1/bi2: 2 
  35.     bi1%bi2: 0 
  36.     bi1的负数: -222222222222222222222 
  37.     bi1为整数 
  38.     bi1大于bi2 
  39. */  

java.math.BigDecimal 精确计算

[java] view plain copy
  1. package com.itlwc;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. public class Test {  
  6.     public static void main(String[] args) {  
  7.   
  8.         BigDecimal bd1 = new BigDecimal("3.8");  
  9.         BigDecimal bd2 = new BigDecimal("0.7");  
  10.         BigDecimal bd3 = new BigDecimal("3.800");  
  11.         System.out.println("bd1 = " + bd1);  
  12.         System.out.println("bd2 = " + bd2);  
  13.         System.out.println("bd3 = " + bd3);  
  14.         System.out.println("bd1 + bd2 = " + bd1.add(bd2));  
  15.         System.out.println("bd1 - bd2 = " + bd1.subtract(bd2));  
  16.         System.out.println("bd1 * bd2 = " + bd1.multiply(bd2));  
  17.         System.out.println("bd1 / bd2 = "  
  18.                 + bd1.divide(bd2, BigDecimal.ROUND_HALF_DOWN));  
  19.         System.out.println("bd3 / bd2 = "  
  20.                 + bd3.divide(bd2, BigDecimal.ROUND_HALF_UP));  
  21.         // 将bd1与bd2进行比较  
  22.         if (bd1.compareTo(bd3) == 0) {  
  23.             System.out.println("bd1 = " + bd1 + " bd3 = " + bd3  
  24.                     + " 则bd1与bd3是相等的!!");  
  25.         }  
  26.   
  27.         // 1  
  28.         BigDecimal bigD1 = BigDecimal.ONE;  
  29.         // 10  
  30.         BigDecimal bigD2 = BigDecimal.TEN;  
  31.         // 0  
  32.         BigDecimal bigD3 = BigDecimal.ZERO;  
  33.   
  34.     }  
  35. }  
  36. /* 
  37. 打印结果: 
  38.     bd1 = 3.8 
  39.     bd2 = 0.7 
  40.     bd3 = 3.800 
  41.     bd1 + bd2 = 4.5 
  42.     bd1 - bd2 = 3.1 
  43.     bd1 * bd2 = 2.66 
  44.     bd1 / bd2 = 5.4 
  45.     bd3 / bd2 = 5.429 
  46.     bd1 = 3.8 bd3 = 3.800 则bd1与bd3是相等的!! 
  47. */  
0 0
原创粉丝点击