zoukankan      html  css  js  c++  java
  • Java日志第27天 2020.8.1

    包装类

    基本数据类型使用起来非常方便,但是没有操作这些数据方法。

    我们可以使用一个类,把基本类型的数据装起来,在类中定义一些方法,这个类就叫做包装类。

    基本数据类型对应的包装类位于java.lang包中。

    注意int类型所对应的包名称是Integer,char类型对应的包名称是character。

    装箱与拆箱

    装箱

    装箱是指把基本类型的数据包装到包装类中。

    构造方法:

    1.Integer( int value ) 构造一个新分配的Integer对象,它表示指定的int值

    public class Demo01Integer {
    
        public static void main(String[] args) {
            
            Integer integer = new Integer(1);
            System.out.println(integer);
            
        }
    }

    2.Integer ( String s ) 构造一个新分配的Integer对象呢,它表示String参数所指示的int值

    public class Demo01Integer {
    
        public static void main(String[] args) {
    
            Integer integer = new Integer("2");
            System.out.println(integer);
    
        }
    }

    传递的字符串必须是基本类型的字符串,否则会抛出异常

    静态方法:

    1.static Integer valueOf ( int i )

    public class Demo01Integer {
    
        public static void main(String[] args) {
    
            Integer integer = Integer.valueOf(2);
            System.out.println(integer);
    
        }
    }

    2.static Integer valueOf(String s)

    public class Demo01Integer {
    
        public static void main(String[] args) {
    
            Integer integer = Integer.valueOf("3");
            System.out.println(integer);
    
        }
    }

    拆箱

    拆箱是指在包装类中取出这些基本类型的数据。

    成员方法:

    int intValue() 以int类型返回该Integer的值

    public class Demo01Integer {
    
        public static void main(String[] args) {
    
            Integer integer = Integer.valueOf("3");
            System.out.println(integer);
    
            int i = integer.intValue();
            System.out.println(i);
    
        }
    }

    自动装箱与自动拆箱

    public class Demo01Integer {
    
        public static void main(String[] args) {
    
            //自动装箱:直接把int类型的整数赋给包装类
            Integer integer = 1;
            
            /*
            自动拆箱
            integer+2进行了自动拆箱处理
            把integer转换为整数类型,再加2
            之后再一次进行装箱处理,把整数赋给包装类
             */
            integer = integer + 2;
    
        }
    }

    由此可知,在进行ArrayList方法增加元素时,运用到自动装箱。

    基本类型与字符串类型之间的转换

    public class DemoMain {
    
        public static void main(String[] args) {
            int i = 100;
            //基本类型 -> 字符串类型
    
            //1.在数据后加一个空字符串
            String s1 = i + "";
            System.out.println(s1+100);//100100,而不是200
    
            //2.包装类中的静态方法
            String s2 = Integer.toString(i);
            System.out.println(s2+100);
    
            //3.String类中的toString(int i)方法
            String s3 = String.valueOf(i);
            System.out.println(s3+100);
    
            System.out.println("==============");
            // 字符串类型 -> 基本类型
            int i1 = Integer.parseInt(s3);
            System.out.println(i1+100);//200,而不是100100
        }
    }

    结果如下:

  • 相关阅读:
    swoole多进程操作
    LinUX系统ThinkPHP5链接MsSQL数据库的pdo_dblib扩展
    php 访问用友u8数据
    C++/CLI剪辑
    托管代码中调用c++本地代码
    非托管代码中调用托管代码
    Resharper快捷键使用
    Unity3d简便的声音管理方案
    QT离线安装包
    Winform中使用Reactivex代替BeginInvoke/Invoke来更新UI数据
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13415312.html
Copyright © 2011-2022 走看看