zoukankan      html  css  js  c++  java
  • Java基础之:包装类(Wrapper)

    Java基础之:包装类(Wrapper)

    java中有八种基本定义相应的引用类型—包装类,例如我们最常用到的Integer,Double等等

    这些类将基本数据类型转换为了引用数据类型,提供了一系列的方法。

     

    包装类与基本数据类型转换

    这里使用Integer ---> int 举例,其他类型是一样的方式。

    提到两个名词,装箱和拆箱。

    简单案例

    package class_wrapper;
    ​
    public class ClassTest {
    ​
        public static void main(String[] args) {
    ​
            int a = 1;
            Integer c = new Integer(a); //手动装箱
            Integer b = new Integer(2);
            
            Integer i = new Integer(2);
            int j = i.intValue();   //手动拆箱
            
            int i1 = 1;
            Integer j1 = i1;    //自动装箱,底层实现与手动方式一样,只是多了封装
            
            Integer a1 = new Integer(1);
            int b1 = a1;//自动拆箱
    ​
            
            Double d = 100.1;   //Double类的自动装箱
            double d1 = d; //自动拆箱
        }
    ​
    }
    

     

    包装类与String类型转化

    这里以Integer --> String为例:

    package class_wrapper;
    ​
    public class SrtingAndWrapper {
    ​
        public static void main(String[] args) {
    ​
            // 包装类型————>String类型
            Integer i = 10;
            // 方式1:
            String s1 = i.toString();
            // 方式2:
            String s2 = String.valueOf(i);
            // 方式3:(推荐使用)
            String s3 = i + "";
            System.out.println(s3);
            // String————>包装类
            // 方式1:(推荐使用)
            Integer j = new Integer(s1);
            // 方式2:
            Integer j2 = Integer.valueOf(s2);
            
            // 基本类型————>String类型
            int a = 10;
            // 方式1:(推荐使用)
            String ss = a + "";
            // 方式2:
            String sss = String.valueOf(a);
    ​
    ​
            //String————>基本类型(除了char类型)
            int aa = Integer.parseInt(ss);
            double dd = Double.parseDouble(ss);
    ​
            char c = ss.charAt(0);// [把字符串转成 char ,是通过charAt(0)]
            char[] cc = ss.toCharArray();   //String底层使用char数组实现,可以通过toCharArray转换
            System.out.println(cc);
    ​
        }
    }

    说明:关于String类型的详解,会在下一篇博客写出。

     

    常用的包装类方法

    这里以Integer和Character为例:

    package class_wrapper;
    public class WarpperMethods {
        public static void main(String[] args) {
            System.out.println(Integer.MIN_VALUE); //返回最小值
            System.out.println(Integer.MAX_VALUE);//返回最大值
                    
            System.out.println(Character.isDigit('a'));//判断是不是数字
            System.out.println(Character.isLetter('a'));//判断是不是字母
            System.out.println(Character.isUpperCase('a'));//判断是不是大写
            System.out.println(Character.isLowerCase('a'));//判断是不是小写
                    
            System.out.println(Character.isWhitespace('a'));//判断是不是空格
            System.out.println(Character.toUpperCase('a'));//转成大写
            System.out.println(Character.toLowerCase('A'));//转成小写
        }
    }
    

      

     

  • 相关阅读:
    Windows核心编程笔记:同步设备I/O与异步设备I/O 200404
    最顶层窗口
    Windows环境变量
    如何使用Beyond Compare比较两个文件夹的差异
    c++生成缩略图
    MD5加密算法
    c++读写注册表
    通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
    Hive中HSQL中left semi join和INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN区别
    postgreSQL格式化时间的函数详解
  • 原文地址:https://www.cnblogs.com/SongHai/p/14159296.html
Copyright © 2011-2022 走看看