• 基本包装类和System类


    基本包装类

    程序界面上用户输入的数据都是以字符串类型进行存储的,要把字符串转成基本数据类型操作

    基本包装类就是对8种基本数据类型进行包装:Integer Character 其他的都是首字母大写

    1、将字符串转换成基本数据类型

    Integer.parseInt(字符串)   Double.parseDouble(字符串)   其他类型也一样

    //parseXXX(String str)
            //传入的字符串必须是对应的基本数据类型
            String str = "12";
            int i = Integer.parseInt(str);
            System.out.println(--i);
            
            String str2 = "2.5";
            double d = Double.parseDouble(str2);
            System.out.println(d);

    2、将基本数据类型转换成字符串

    用+号拼接一个空字符串""

    用String类中的valueof()方法

    用包装类中的toString(参数)方法

    public static void main(String[] args) {
            //基本数据类型+""   加号的拼接
            int i = 9;
            String str = i+"";
            System.out.println(str+1);
            //调用String类的valueof()方法
            String str1 = String.valueOf(1);
            System.out.println(str1+9);
            //调用包装类中的toString(参数)方法
            String str2 = Integer.toString(5);
            System.out.println(str2+5);
        }

    3、基本数据类型和包装类的转换

    jdk1.5以前的方法

    基本数据类型--包装类

    构造方法

    valueof()方法

    Integer in = new Integer(3);
                    Integer in2 = new Integer("3");
                    Integer in3 = Integer.valueOf(5);
                    Integer in4 = Integer.valueOf("5");

    包装类--基本数据类型

    调用intValue()方法

    int i = in.intValue();

    jdk1.5以后

    自动装箱和拆箱

    自动装箱:基本数据类型自动直接转成对应的包装类对象

    自动拆箱:包装类对象自动直接转成对应的基本数据类型

    public static void method1(){
            //自动装箱
            Integer in = 5;//相当于Integer in = new Integer(3);
            //自动拆箱
            //int sum = in + 6;
            System.out.println(in+6);
            //System.out.println(sum);
            
        }

    在自动拆装箱中,遇到byte类型的数值以内,先创建一个对象,后来的对象都指向第一个对象的地址

    public static void method22(){
            //在自动拆装箱中,如果是byte(128)数值以内,先创建一个对象,后来的对象都指向第一个对象的地址
            Integer in = 20;
            Integer in2 = 20;
            System.out.println(in==in2);//true
            System.out.println(in.equals(in2));//true
        }
  • 相关阅读:
    Matlab之rand(), randn(), randi()函数的使用方法
    matlab给图片插入说明文字
    matlab之find()函数
    excel根据数据源变化的动态图表
    高斯坐标
    (转)Mysql技术内幕InnoDB存储引擎-表&索引算法和锁
    (转)MySQL 插入数据时,中文乱码问题的解决
    (转)防止人为误操作MySQL数据库技巧一例
    (转)mysql explain详解
    (转)一个MySQL 5.7 分区表性能下降的案例分析
  • 原文地址:https://www.cnblogs.com/yelena-niu/p/9092490.html
Copyright © 2020-2023  润新知