zoukankan      html  css  js  c++  java
  • 基本类型和包装类

    包装类的产生就是为了解决基本数据类型没有属性,方法,不能对象化交互的问题

    基本数据类型和包装类之间的对应关系如下:

    基本类型——包装类

    byte——Byte

    short——Short

    int——Integer

    long——Long
    float——Float

    double——Double

    char——Character

    booleam——Boolean

    1. 包装类与基本数据类型装换的装箱和拆箱

    package wrap;
    
    public class WrapTestOne {
        public static void main(String[] args) {
            //装箱:把基本数据类型转换成包装类
            //1. 自动装箱
            int t1 = 2;
            Integer t2 = t1;   //把t1直接赋值给t2
            //2. 手动装箱
            Integer t3 = new Integer(t1);
    
            //测试
            System.out.println("int类型变量t1的值:"+t1);
            System.out.println("Integer类型对象t2的值:"+t2);
            System.out.println("Integer类型对象t3的值:"+t3);
    
    
            //拆箱:把包装类转换成基本数据类型
            //1. 自动拆箱
            int t4 = t2;
            //2. 手动拆箱
            int t5 = t2.intValue();    //把Integer对象t2转换成int类型的值
    
            System.out.println("Integer对象t2的值:"+t2);
            System.out.println("自动拆箱后,int类型变量t4的值:"+t4);
            System.out.println("手动拆箱后,int类型变量t5的值:"+t5);
    
            double t6 = t2.doubleValue();  //把t2转换成double了类型的值
            System.out.println(t6);
        }
    }

    2. 基本数据类型和字符串之间的转换

    package wrap;
    
    public class WrapTestTwo {
        public static void main(String[] args) {
            //基本数据类型转换为字符串
            int t1 = 2;
            String t2 = Integer.toString(t1);  //使用toString方法,把int类型转换为String
    
            //测试
            System.out.println("int类型转换为String类型对象t2="+t2);
    
            //字符串转换为基本数据类型
            int t3 = Integer.parseInt(t2);   //使用Integer的parseInt方法,把字符串转换成int类型
            System.out.println("String类型转换为int类型变量t3= "+t3);
            int t4 = Integer.valueOf(t2);  //把String转换成Integer类型后,自动拆箱操作变成int类型
            System.out.println(t4);
    
        }
    }

    3. 包装类的默认值

        String name;  //包装类和基本类型默认值都是null
    int mouth; //默认值为0
    Integer mouth1; //默认值为null
    double weight; //默认值为0.0
    Double weight1; //默认值为null

    4. 包装类的比较

    package wrap;
    
    public class WrapperTest {
        public static void main(String[] args) {
            Integer one = new Integer(100);
            Integer two = new Integer(100);
            System.out.println(one == two);   //false:等号两边放对象名,比较的是引用对象指向的不同的内存空间
    
            Integer three = 100;   //自动装箱
            System.out.println(three == 100);  //比较时自动拆箱  100==100 true
    //        Integer foure = 100;  ====等于(-128————127之间隐式调用)=====     Integer foure = Integer.valueOf(100);
            Integer foure = Integer.valueOf(100);
            System.out.println(three == foure);    //隐式调用,因此指向的是内存的同一块空间
    
            Integer five = 200;
            System.out.println(five == 200);
            Integer six = 200;
            System.out.println(five == six);  //false 超过隐式调用的区间了
        }
    
    }

    注意:float和double类型不具备常量池概念,也就是int中的隐式调用




  • 相关阅读:
    06.章节页面接口开发
    05.课程主页面三个接口开发
    python高级(六)——用一等函数实现设计模式
    python高级(五)—— python函数(一等对象)
    python高级(四)—— 文本和字节序列(编码问题)
    python高级(三)—— 字典和集合(泛映射类型)
    python高级——目录
    python高级(二)—— python内置序列类型
    python高级(一)—— python数据模型(特殊方法)
    python实现百度地图API获取某地址的经纬度
  • 原文地址:https://www.cnblogs.com/mpp0905/p/10371973.html
Copyright © 2011-2022 走看看