Java面向对象(六)

打印 上一主题 下一主题

主题 893|帖子 893|积分 2679

Java面向对象(六)


目录

十九、包装类

19.1 八种基本类型包装类

java 提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征。

19.2 基本类型、包装类与 String 类间的转换。


19.3 基本数据类型转换为包装类(装箱)
  1. public void test1(){
  2.         public static void main(String[] args){
  3.                
  4.                 int num1 = 10;
  5. //                System.out.println(num1.toString());                报错
  6.                 Integer in1 = new Integer(num1);                        //通过构造器装箱
  7.                 System.out.println(in1.toString());
  8.                
  9.                 Integer in2 = new Integer("123");                        //通过字符串参数装箱
  10.                 System.out.println(in2.toString());
  11.    
  12.                
  13.                 //报异常
  14. //                Integer in3 = new Integer("123abc");
  15. //                System.out.println(in3.toString());
  16.                
  17.                 Float f1 = new Float(12.3f);                                //通过构造器装箱
  18.                 Float f2 = new Float("12.3");                                //通过字符串参数装箱
  19.                 System.out.println(f1);
  20.                 System.out.println(f2);
  21.                
  22.                 Boolean b1 = new Boolean(true);
  23.                 Boolean b2 = new Boolean("TrUe");
  24.                 System.out.println(b2);                                                //true
  25. /*                b2 返回 true 原因:
  26.                    public Boolean(String s) {
  27.                 this(parseBoolean(s));
  28.             }
  29.            
  30.                 public static boolean parseBoolean(String s) {
  31.                 return ((s != null) && s.equalsIgnoreCase("true"));
  32.             }
  33.            
  34.                 equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。
  35.                 如果给定对象与字符串相等,则返回 true,否则返回 false。
  36. */
  37.                 Boolean b3 = new Boolean("true123");
  38.                 System.out.println(b3);                                                //false       
  39.                
  40.                 Order order = new Order();
  41.                 System.out.println(order.isMale);                        //false(基本数据类型默认值)
  42.                 System.out.println(order.isFemale);                        //null(引用数据类型默认值)
  43.     }
  44. }
  45. class Order{
  46.         boolean isMale;
  47.         Boolean isFemale;
  48. }
复制代码
19.4 包装类转换为基本数据类型(拆箱)
  1. public void test2(){
  2.     public static void main(String[] args){
  3.                
  4.         Integer in1 = new Integer(12);
  5.                
  6.                 int i1 = in1.intValue();                        //调用类方法xxxValue()
  7.                 System.out.println(i1 + 1);                        // 13
  8.                
  9.                
  10.                 Float f1 = new Float(12.3);
  11.                 float f2 = f1.floatValue();                        //调用类方法xxxValue()
  12.                 System.out.println(f2 + 1);                        // 13.3
  13.     }
  14. }
复制代码
19.5 自动装箱拆箱
  1. //        JDK 5.0 新特性:自动装箱 与自动拆箱
  2. public void test3(){
  3.                 int num1 = 10;
  4.                 Integer in1 = num1;        //自动装箱
  5.                
  6.                 boolean b1 = true;
  7.                 Boolean b2 = b1;        //自动装箱
  8.                
  9.                 System.out.println(in1.toString());
  10.    
  11.                 int num3 = in1;                //自动拆箱
  12.                
  13. }
复制代码
19.6 基本数据类型、包装类转换为String类型
  1. // String类型:调用String重载的valueOf(Xxx xxx)
  2. public void test4(){
  3.         public static void main(String[] args){       
  4.                 int num1 = 10;
  5.                 //方式1:连接运算
  6.                 String str1 = num1 + "";
  7.         
  8.                 //方式2:调用String的valueOf(Xxx xxx)
  9.                 float f1 = 12.3f;
  10.                 String str2 = String.valueOf(f1);       
  11.                
  12.                 Double d1 = new Double(12.4);
  13.                 String str3 = String.valueOf(d1);
  14.                 System.out.println(str2);                        //"12.3"
  15.                 System.out.println(str3);                        //"12.4"
  16.     }       
  17. }
复制代码
19.7 String类型转换为基本数据类型、包装类
  1. // 调用包装类的parseXxx(String s)
  2. public void test5(){
  3.         public static void main(String[] args){
  4.         
  5.                 String str1 = "123";
  6.                 //错误的情况:
  7. //                int num1 = (int)str1;
  8. //                Integer in1 = (Integer)str1;
  9.                
  10.                 int num2 = Integer.parseInt(str1);
  11.         //当字符串中不是纯数字时,可能会报NumberFormatException
  12.                 System.out.println(num2 + 1);
  13.                
  14.                 String str2 = "true1";
  15.                 boolean b1 = Boolean.parseBoolean(str2);
  16.                 System.out.println(b1);                //false
  17.     }
  18. }
复制代码
19.8 特殊例子
  1. public void test6(){
  2.         public static void main(String[] args){
  3.         
  4.                 Object o1 = true ? new Integer(1) : new Double(2.0);
  5.                 System.out.println(o1);
  6.         //        输出 1.0,三元运算符要求两边表达式数据类型一致,左边 Integer 类型自动类型转换为 Double      
  7.         
  8.     }
  9. }
复制代码
  1. public void method1() {
  2.     public static void main(String[] args){
  3.                 Integer i = new Integer(1);
  4.                 Integer j = new Integer(1);
  5.                 System.out.println(i == j);                //false
  6.                 Integer m = 1;
  7.                 Integer n = 1;
  8.                 System.out.println(m == n);                //true
  9.                 Integer x = 128;                                //相当于new了一个Integer对象
  10.                 Integer y = 128;                                //相当于new了一个Integer对象
  11.                 System.out.println(x == y);                //false
  12.     }
  13. }
  14. /*
  15.         Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了。
  16.         目的:提高效率
  17.         详细可以看 Integer 源码
  18. */
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表