Java常用类复习

一.基本数据类型的装箱与拆箱

1.为什么需要包装类?

万物皆对象,有些方法需要调用的是对象,而并非基本数据类型

2.特点

自动装箱和自动拆箱只发生在编译阶段

        Integer integerNum = 100; // 进行自动装箱,得到的是封装类
        int intNum = integerNum; // 进行自动拆箱,得到基本数据类型
3.装箱:
     
      把基本类型转换为包装类的过程就是装箱
      Integer b =1;
     自动装箱,底层其实执行了Integer i=Integer.valueOf(10);
4.拆箱:
     把包装类转换为基本数据类型就是拆箱
     自动拆箱,底层其实执行了int n=i.intValue();
     jdk1.5 之后出现了 自动装箱与拆箱
     原理 就是通过包装类的valueOf方法实现的
     整数和浮点数的包装类型都继承自Number;

二.日期类的使用

1.SimpleDate

作用:用来对日期Date类的格式化和解析

1.格式化 日期-->字符串

2.解析 字符串-->日期

   public  void test1() throws ParseException {
//
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        格式化
        System.out.println(sf.format(date));//2022-07-14 07:55:02
//        解析 要求字符串能被SimpleDateFormat识别
        System.out.println(sf.parse("2022-07-14 07:55:02"));
        //Thu Jul 14 07:55:02 CST 2022


    }

2.Calendar

说明:日历类 抽象类 。所以只能调它的子类对象进行实例化

实例化

 Calendar calendar = Calendar.getInstance();

常用方法

//        get()
        int days = calendar.get(Calendar.DAY_OF_WEEK);
//        获取几天是周几 周日是1
        System.out.println(days);//5
//        获取今天是这月份的多少天 等方法
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//14
//        set()  calendar可变性不太好
//        把现在改成这个月后的15号
        calendar.set(Calendar.DAY_OF_MONTH,15);
//        获取修改之后的
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//15
//        add() 
        calendar.add(Calendar.HOUR_OF_DAY,-3);
//        把现在的时间 -3个小时
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));

3.日期时间API的迭代

jdk1.0 Date
jdk1.1 Calender
jdk1.8 提出了新的一套API 主要在 Java.time包下

4.Instant

时间上的一个瞬时点 1970年1月1日00:00:00 开始的UTC开始的秒数   类似于java.util.Date类 Java中以毫秒为单位

@Test
    public void  test7(){
    Instant now = Instant.now();

    System.out.println(now);
    //2022-07-14T12:17:40.265Z
    long l = now.toEpochMilli();
//    毫秒数
    System.out.println(l);
//    1658400154167
}

5.DateTimeFormat

作用:格式化或解析日期时间,类似于SimpleDateFormat

    @Test
    public void test9(){
//        实例化
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy-MM-dd hh:mm:ss");
//        常用方法
//        格式化
        System.out.println(dtf.format(LocalDateTime.now()));
//        解析
        System.out.println(dtf.parse("22-07-14 08:33:38"));
//       {MicroOfSecond=0, SecondOfMinute=38, NanoOfSecond=0, HourOfAmPm=8, //MinuteOfHour=33, MilliOfSecond=0},ISO resolved to 2022-07-14 }

三.排序

1.使用背景

需要对多个对象排序即比较大小  使用两个接口其中的一个即可 Comparable 和Comparator

2.Comparable

特点:自然排序 java.lang.Comparable

使用方法

1.实现该接口

2.重写compareTo方法

对于自定义类来使用

public class ComparableTe implements Comparable {
    private String name;
    private Integer age;

    //    比到最后 最终归属于基本数据类型的比较
    @Override
    public int compareTo(Object o) {
//       1.先判断两个地址值是否相同
        if (this == o) {
            return 1;
        } else {
//            2.看o是不是这个类的对象
            if (o instanceof ComparableTe) {
                return -1;
            } else {
//            3.把o强转为此类对象
                ComparableTe com = (ComparableTe) o;
//                4.取值进行比较(当然比较这个字段如果不能满足 还可以比较其它的字段)
                if (this.age == com.age) {
                    return 0;
                } else {
                    return this.age > com.age ? 1 : -1;
                }
            }
        }
    }

3. Comparator

特点: 定制排序  java.util.Comparator

使用背景:你所需要的类 没有实现Comparable接口 或者已经实现但不是你想要的排序

使用方法:

1.实现该接口

2.重写compare方法

@Test
    public void te10() {
        int[] a = new int[]{1, 3, 2, 5};
        Integer[] a1 = new Integer[]{1, 3, 2, 5};
        Arrays.sort(a);
        for (int b :
                a) {
            System.out.print(b + "  ");
//        1  2  3  5
        }
        //            此处 a1不能是int 数组
        Arrays.sort(a1,
                new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {
                        int n1 = (int)  o1;
                        int n2 = (int)  o2;
                        return n1<=n2 ? 1 : -1;
                    }


                });
        for (int b :
                a1) {
            System.out.print(b + "  ");
//        5  3  2  1
        }

    }

四.System类

1.特点

1.代表系统,系统的很多属性和控制方法都放置再该类的内部,该类位于java.lang包

2.构造器是private的,无法造对象,其内部的成员变量和方法都是static的

3. in标准的输入流,out标准的输出流

4.public final class System

5.获取一些java的版本信息什么的

2.测试(不常用)

    @Test
    public void test1(){
       String jv= (String) System.getProperty("java.version");
//       获取java的版本
        System.out.println(jv);

        //1.8.0_231
    }

五.Math类

1.特点

1.提供了一系列静态方法用于计算 其方法的参数一般是float 里面都是静态的,不用造对象去调 2.BigInteger java.math包下的 可以表示不可变的任意精度的整数 用于范围较大的数

3.BigDecimal java.math包下的 精度要求比较高的话可以用这个

2.常用的方法

    @Test
    public void te(){
        int a;
//        Math.random() 取随机数   返回的的是[0-1)
        for (int i = 0; i <10 ; i++) {
            a = (int) (Math.random()*10);
                System.out.print(a+" ");
//                9 2 5 7 7 5 4 4 9 8
        }
        //Math.max 同类型的进行比较 int long float double四种
        System.out.println(Math.max(1, 100));
        //100
        System.out.println(Math.round(4.9));
        //5
        System.out.println(Math.round(5.2));
        //5

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值