zoukankan      html  css  js  c++  java
  • java基础之包装类型

                                                                        包装类型
    引入该类型的原因
          因为基本数据类型不具备对象的特性,不能调用方法,所以有时需要将其转换为包装类。

    包装类型有两大类方法
          1.将本类型和其它基本类型进行转换方法。
          2.将字符串和本类型及包装类型互相转换的方法。

     1 package packageType;
     2 
     3 public class Demo01 {
     4     public static void main(String[] args) {
     5         // 定义int类型变量score1,值为86
     6         int score1=66;
     7 
     8         //创建Integer包装类对象,表示变量score1的值  装箱
     9         Integer score2=new Integer(score1);
    10 
    11         //将Integer包装类型转换为double类型     拆箱
    12         double score3=score2.doubleValue();
    13 
    14         //将Integer包装类转换为float类型
    15         float score4=score2.floatValue();
    16 
    17         //将Integer包装类转换为int类型
    18         int score5=score2.intValue();
    19 
    20         System.out.println("Integer包装类:"+score2);
    21         System.out.println("double包装类:"+score3);
    22         System.out.println("float包装类:"+score4);
    23         System.out.println("int包装类:"+score5);
    24     }
    25 }
    26 /*
    27 Integer包装类:86
    28 double包装类:86.0
    29 float包装类:86.0
    30 int包装类:86
    31 * */
    //几种基本类型操作也是一样的

    1
    package packageType; 2 3 public class Demo02 { 4 public static void main(String[] args) { 5 // 定义double类型变量 6 double a = 91.5; 7 8 // 手动装箱 9 Double b = new Double(a); 10 11 // 自动装箱 12 Double c = a; 13 14 System.out.println("装箱后的结果为:" + b + "和" + c); 15 16 // 定义一个Double包装类对象,值为87.0 17 Double d = new Double(87.0); 18 19 // 手动拆箱 20 double e = d.doubleValue(); 21 22 // 自动拆箱 23 double f =d; 24 25 System.out.println("拆箱后的结果为e,f:" + e + "和" + f); 26 } 27 }

    /*
    装箱后的结果为:91.5和91.5
    d:87.0
    拆箱后的结果为:87.0和87.0
    * */

    拆装箱?

          基本类型转换为包装类型叫装箱,包装类型转换为基本类型叫拆箱。 

    将基本数据类型和字符串之间进行转换


    其中,基本类型转换为字符串有三种方法:
        1.用包装类的toString方法
        2.使用String类的valueOf()方法
        3.用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串。

    eg1:

       //将基本类型转换为字符串
       int c=10;
       //转换为包装类型,再转换为字符串输出
       String str1=Integer.toString(c);
       String str3=c+"";

    eg2:

       //将字符串转成基本类型
       1.调用parseXxx静态方法
       //将字符串类型转换成基本类型
       String str=8;
       int d=Integer.parseInt(str);

     1 package packageType;
     2 
     3 public class Demo03 {
     4      public static void main(String[] args) {
     5 
     6         double m = 78.5;
     7         //将基本类型转换为字符串
     8         String str1 =Double.toString(m);
     9 
    10         System.out.println("m 转换为String型后与整数20的求和结果为: "+(str1+20));
    11 
    12         //将字符串转换成基本类型的两种方法
    13         String str = "180.20";
    14         // 将字符串转换为基本类型:调用包装类的parseXxx静态方法
    15         Double a =Double.parseDouble(str);                        ;
    16 
    17         System.out.println("str 转换为double型后与整数20的求和结果为: "+(a+20));
    18     }
    19 }
    成年人的世界没有那么多的童话,也没有那么多的逆袭。
  • 相关阅读:
    sql server 函数
    Idea+Maven+Jetty,代码修改后,重新部署,无法获取新内容~
    css优先级
    jquery选择器
    XML学习笔记一
    JVM参数调优实例解析
    JVM中的Stack和Heap--------堆与栈
    svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“
    Spark中的CombineKey()详解
    Spark性能优化指南——高级篇
  • 原文地址:https://www.cnblogs.com/shijinglu2018/p/10327140.html
Copyright © 2011-2022 走看看