zoukankan      html  css  js  c++  java
  • 包装类

    包装类

    jdk5.0之后增加自动拆箱和自动装箱

    • 自动装箱
    int num = 3;
    Integer in = num; //编译可以通过 ,拆箱写法
    Integer in1 = new Integer(num); //不拆箱写法
    
    • 自动拆箱
    Integer in1 = new Integer(3);
    int i = in1; //自动拆箱
    
    • 基本数据类型,包装类型转为string类型
    //1. 方式1
    int a = 3;
    String s1 = a + "";
    //2. 方式2 
    String s2 = String.valueOf(a);
    
    • string类型转为基本数据类型,包装类型: parseXXX
    int a = Integer.parseInt(3); // a = 3
    boole b = Boolean.parseBoolean("true"); //b = true
    
    //Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128~127范围的整数。
    //如果我们使用自动装箱的方式,给Integer赋值的范围在
    //-128~127范围内时,可以直接使用数组中的元素,不用再去new了。
    Integer a = 1;
    Integer b = 1;
    System.out.println(a == b); //true;
    
    Integer a1 = 128;
    Integer b1 = 128;
    System.out.println(a1 == b1); //false;
    
  • 相关阅读:
    C++总结
    Perl注释格式
    处理压力测试中的问题
    C++标准库中的时间函数
    C语言中如何使用宏 转载
    探索C++的秘密之详解extern "C"
    调试代码的技巧
    又长一岁
    [转]pycharm的一些快捷键
    dizhi
  • 原文地址:https://www.cnblogs.com/huyuqing/p/14320317.html
Copyright © 2011-2022 走看看