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

    包装类

    装箱和拆箱

    示例代码:

    public class Demo2 {
        public static void main(String[] args) {
            //JDK1.5之前
            //1.装箱操作,基本类型转为引用类型的过程
            int num1 = 18; //基本类型数据
            //使用Integer类创建一个对象
            Integer integer = new Integer(num1);
            Integer integer1 = Integer.valueOf(num1);
    
            //2.拆箱操作,引用类型转为基本类型
            Integer integer2 = new Integer(100);
            int num2 = integer2.intValue();
    
    
            //JDK1.5之后,提供了自动装箱和拆箱
            int age = 21;
            //自动装箱
            Integer integer3 = age;
            //自动拆箱
            int age1 = integer3;
        }
    }

    基本类型和字符串转换

    示例代码:

    public class Demo2 {
        public static void main(String[] args) {
            //基本类型和字符串之间的转换
            //1.基本类型转字符串
            int a = 15;
            //1.1  使用+号
            String s1 = a+"";
            System.out.println(s1);
            //1.2  使用Integer中的toString()方法
            String s2 = Integer.toString(a);
            String s3 = Integer.toString(a,16);  //toString()方法重载,转换为16进制的形式
            System.out.println(s2);
            System.out.println(s3);
            System.out.println("------------------------------------------------------------------------------------");
            //2.字符串转基本类型
            String str = "100";
            //2.1  使用parseXXX();
            int i = Integer.parseInt(str);
            System.out.println(i);
            System.out.println("------------------------------------------------------------------------------------");
            //boolean字符串形式转为基本类型,"true" -> true  非"treu" -> false
            String string = "true";
            boolean b = Boolean.parseBoolean(string);
            System.out.println(b);
            String string1 = "abcd";
            boolean c = Boolean.parseBoolean(string1);
            System.out.println(c);
        }
    }
  • 相关阅读:
    3 Redis 的常用五大数据类型
    Oracle12C 基本操作和使用
    CentOS7安装VNC服务
    CentOS7.6 安装Oracle12C(下)
    CentOS7.6 安装Oracle12C(上)
    博主创建了一个AGV吧的Discuz,欢迎各位加入进来
    六、openTCS4.17汉化版源码包含通信DEMO,gitee地址见内容
    毕马威图形数独
    五、OpenTCS4.12的模拟运行
    四、通过Socket实现跟AGV小车通信(仅做Demo演示,跟实际工厂运行无关)
  • 原文地址:https://www.cnblogs.com/qiudajiang/p/13245926.html
Copyright © 2011-2022 走看看