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);
        }
    }
  • 相关阅读:
    阿里的GCIH技术
    java栈
    hotsport jvm后台线程包括哪些
    java运行时数据区
    java双亲委派
    获取类加载器方式
    用户自定义类加载器(java防止反编译)
    JSP-07-使用JavaBean封装数据
    JSP-06-使用JDBC操作数据库
    InstallShield 下载安装
  • 原文地址:https://www.cnblogs.com/qiudajiang/p/13245926.html
Copyright © 2011-2022 走看看