常用类

一、包装类

  包装类即是基本数据类型对应的引用数据类型。

  Byte、Short、Integer、Long、Float、Double、Character、Boolean

  主要使用的方法:基本类型、引用类型、String间的相互转换。

 

/**
*包装类,测试各种类型间的相互转换 *基本数据类型 <-------->其包装类<------->String * */ public class WrapperClass { public static void main(String[] args) { // 1、int 转 Integer int a = 2; Integer inte1 = Integer.valueOf(a); System.out.println(inte1); //2、Integer 转 int int b = inte1.intValue(); System.out.println(b); //3、int 转 String String str = String.valueOf(b); System.out.println(str); //4、String 转 int int c = Integer.parseInt(str); System.out.println(c); //5、Integer 转 String String str2 = inte1.toString(); System.out.println(str2); //6、String 转 Integer Integer inte2 = Integer.valueOf(str2); System.out.println(inte2); } }

  注意:

  1、Boolean、Character类继承Object类,其他包装类均继承于Number类

  2、jdk5之后提供自动拆装箱,自动装箱:如Integer inte = 1;自动拆箱:int  a = new Integer(“1”);

  3、 享元模式:Short,Byte,Integer,Long的几个包装类内部把常用的-128-127的数缓存起来

 //享元模式
 Integer i1=125;
 Integer i2=125;
 System.out.println(i1 ==i2);//true
        
 Integer i3=128;
 Integer i4=128;
 System.out.println(i3 ==i4);//false

 

二、System类

  该类为工具类,不能实例化对象,方法全部是static修饰。

  主要方法:

  1、System. arraycopy(Object src, int srcPos, Object dest, int destPos, int length);//复制数组

       参数:src:源数组

                 srcPos:原数组复制起始位置

                dest:目标数组

                destPos:目标数组起始复制位置

                length:复制的长度

   

      int[] arr1 ={1,4,7};
       int[] arr2 ={3,6,9};
       int[] arr3 =new int[6];
       System.arraycopy(arr1, 0, arr3, 0, 3);//复制第一个数组
       System.arraycopy(arr2, 0, arr3, 3, 3);//复制第二个数组
       System.out.println(Arrays.toString(arr3));//[1, 4, 7, 3, 6, 9]

 

  2、long System.currentTimeMillis();//获取当前时间毫秒

  3、下面两个方法最好不要用

    System.gc();

    System.exit(0);

三、字符串相关类

  String:不可变字符串

  StringBuffer/StringBuilder:可变字符串(16)

  场景选择:如果对字符串不做过多的增删改操作一般用String,如果经常对字符串做增删改操作,一般使用StringBuffer/StringBuilder。在多线程中,如果对线程安全有要求则使用StringBuffer,如果是单线程或者多线程中对线程安全没有要求则使用StringBuilder。

  常量池问题:
   1. 作用:提高程序运行性能
   2. 原理:第一次创建(必须是new的时候)会先从常量池中查找,看看是否有改要创建的对象,如果有,那么直接将常量池中的地址复制一份给堆中的对象,如果没有,就在常量池中创建一个对象然后将常量池中对象地址复制一份,给堆中对象,然后将堆中对象地址复制一份给栈中的变量。补充:只要是创建字面量,都是在常量池中,变量保存的值也是指向常量池

   3. 转换为字符串的方式:toString() > String.valueOf(...) > +(字符串拼接:底层是调用的StringBuilder的append())
   4、缓存问题:(常量池、静态区、缓存区):用空间换时间 ,目的提高程序运行效率,既然有缓存一般是可以调整缓存空间大小

 

四、数学相关类

4.1 Math

       当实际需求需要使用到数学相关时,可以在Math类中找方法。

       static double ceil(double a):天花板,向上取整

       static double floor(double a):地板,向下取整

       static long round(double a):四舍五入

    //Math类,是一个工具类,私有化构造方法,static方法
       System.out.println(Math.PI);//字段pi
       System.out.println(Math.ceil(1.5));//天花板,2.0
       System.out.println(Math.floor(1.5));//地板,1.0g
       System.out.println(Math.round(1.5));//四舍五入,2
       System.out.println(Math.rint(1.7));//返回一个最接近的整数,2.0
       System.out.println(Math.rint(2.5));//如果同时接近时,返回偶数,2.0

 

4.2 BigInteger/BigDecimal

       BigInteger:当long不能满足需求时使用

       BigDecimal:当double不能满足需求,要求更高精度时使用

       注意:在操作各种运算时,均是通过调用方法,而不是直接进行加减乘除。

    //BigInteger/BigDecimal
       BigInteger bi = new BigInteger("123");
       bi=bi.add(new BigInteger("123"));//+
       System.out.println(bi);
       bi=bi.subtract(new BigInteger("123"));//-
       System.out.println(bi);
       bi=bi.multiply(new BigInteger("2"));//*
       System.out.println(bi);
       bi=bi.divide(new BigInteger("2"));// /
       System.out.println(bi);

 

五、日期时间相关类

5.1 Date/SimpleDateFormat

       Date:表示当前时间,精确到毫秒

    //Date类
        Date date = new Date();
        System.out.println(date);//Fri Nov 02 15:19:10 CST 2018
        //通过当前时间毫秒数创建date
        long time = System.currentTimeMillis();
        Date date2 = new Date(time);
        System.out.println(date2);//Fri Nov 02 15:20:54 CST 2018
        /*
         * 常用方法
         * after(Date when) 测试此日期是否在指定日期之后。
         * before(Date when) 测试此日期是否在指定日期之前。
         * getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
         * setTime(long time) 设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
         * 
         * */

 

  SimpleDateFormat :由于直接打印出的日期时间不符合我们的习惯,所以使用该格式化类,对其设置格式。

       主要有两个操作:

       1、将Date转换成String

     //将Date转换成String
        //通过无参构造,分步操作
        SimpleDateFormat sdf = new SimpleDateFormat();//创建格式化对象
        sdf.applyPattern("yyyy-MM-dd HH-mm-ss");//设置格式
        String str = sdf.format(date);//格式化date对象
        System.out.println(str);//2018-11-02 15-28-52
        //通过带格式的构造,一步到位
        SimpleDateFormat sdf2 =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;//带格式模板的对象
        String str2 = sdf.format(date2);//格式化对象
        System.out.println(str);//2018-11-02 15-31-56

 

  2、将String解析成Date

     //String解析成Date
        SimpleDateFormat sdf3 =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;//带格式模板的对象
        String str3 = "2019-10-02 14-31-56";
        Date date3 =sdf3.parse(str3);//解析字符串
        System.out.println(date3);//Wed Oct 02 14:31:56 CST 2019

 

5.2 Calendar

       日历类,当需要对年、月、日等等进行操作时,可以考虑使用这个类,而不是去通过操作时间毫秒的方式。

       方法:

       1、abstract void add(int field, int amount)  在原字段值上增减值

       2、void set(int field, int value) 将日历指定字段设置值。

       3、void setTime(Date date) 使用给定的日期设置此日历

       4、Date getTime() 通过日历对象获得当前日期对象

     /*
         * Calendar是一个抽象类,但提供了获取该类对象的方法
         * 类中有一些字段表示年  月  日 这些东西的标识,和操作的方法
         * */
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));//2018年
        System.out.println(calendar.get(Calendar.MONTH));//10(11月,日历使用0-11表示)
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//2号
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));//16时
        System.out.println(calendar.get(Calendar.MINUTE));//7分
        System.out.println(calendar.get(Calendar.SECOND));//26秒
        
        calendar.set(Calendar.YEAR, 2019);//给指定字段设置值
        System.out.println(calendar.get(Calendar.YEAR));//2019
        calendar.add(Calendar.YEAR, -1);//给指定字段增减值
        System.out.println(calendar.get(Calendar.YEAR));//2018
        calendar.setTime(date3);
        Date date4 = calendar.getTime();
        System.out.println(date4);//Wed Oct 02 14:31:56 CST 2019

 

六、随机数

  四种获取随机数的方式

  1、Math.random(不推荐使用,每次用还要计算)

  2、Random(推荐)

  3、ThreadLocalRandom(推荐推荐推荐)

  4、UUID(常用来做数据库主键)

     /*
         * 生成随机数的四种方式
         * */
        //1、不方便使用,每次都要计算,不推荐
        double r1 = Math.random();//返回[0.0,1.0)
        System.out.println(r1);//0.027980544204453728
        //2、推荐
        Random r2 = new Random();
        System.out.println(r2.nextInt());//返回int最小值到最大值范围的随机数,不推荐
        System.out.println(r2.nextInt(10));//返回[0,n),推荐
        //3、强烈推荐
        ThreadLocalRandom tlr = ThreadLocalRandom.current();
        System.out.println(tlr.nextInt(10, 20));//可以生成指定范围的随机数
        //4、uuid以后学数据库可以使用到,一般用做数据库存储主键
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);//10c58f6a-1889-499b-8ec2-f202ab5ebd10

 

posted @ 2018-11-02 17:57  爱吃辣子鸡  阅读(134)  评论(0编辑  收藏  举报