Java基础复习_day11_常用的API

Math

1. 概述

  • Math 包含执行基本数字运算的方法;

2. 调用方式

  • Math类中无构造方法,但内部的方法都是静态的
  • 通过 类名.方法名 进行调用

3. 常用方法

  • public static int abs(int a)返回参数的绝对值
  • public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数
  • public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数
  • public static int round(float a)按照四舍五入返回最接近参数的int
  • public static int max(int a,int b)返回两个int值中的较大值
  • public static int min(int a,int b)返回两个int值中的较小值
  • public static double pow (double a,double b)返回a的b次幂的值
  • public static double random()返回值为double的正值,[0.0,1.0)

System

1. 常用方法

  • public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
  • public static long currentTimeMillis() 返回当前时间(以毫秒为单位)

Object

1. 概述

  • Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份;

2. toString方法

  • 以良好的格式,更方便的展示对象中的属性值
class Student extends Object {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
	// 重写toString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class ObjectDemo {
    public static void main(String[] args) {
        Student s = new Student();
        s.setName("林青霞");
        s.setAge(30);
        System.out.println(s); // Student{name='林青霞', age=30}
    }
}

3. equals方法

① 作用

  • 用于对象之间的比较,返回true和false的结果;
  • 举例:s1.equals(s2); s1和s2是两个对象;

②: 应用场景

  • 不希望比较对象的地址值,想要结合对象属性进行比较的时候。
class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
	// 重写equals方法
    @Override
    public boolean equals(Object o) {
        //this -- s1
        //o -- s2
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o; //student -- s2

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }
}
public class ObjectDemo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("林青霞");
        s1.setAge(30);

        Student s2 = new Student();
        s2.setName("林青霞");
        s2.setAge(30);

        //需求:比较两个对象的内容是否相同
        System.out.println(s1.equals(s2));
    }
}

Arrays工具类

1. 工具类的设计思想

  • 构造方法用 private 修饰
  • 成员用 public static 修饰

2. 常用方法

  • public static String toString(int[] a)返回指定数组的内容的字符串表示形式;
  • public static void sort(int[] a)按照数字顺序排列指定的数组;
public class MyArrays {
    public static void main(String[] args) {
        // 1.toString方法
        int[] arr = {1, 2, 3, 4, 5};
        String s = Arrays.toString(arr);
        System.out.println(s);// [1, 2, 3, 4, 5]

        // 2. sort方法
        int[] arr1 = {5, 3, 2, 1, 6, 4};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));// [1, 2, 3, 4, 5, 6]
    }
}

3. 冒泡排序

package com.myAPI;

import java.util.Arrays;

public class MyArrays {
    public static void main(String[] args) {
    
        int[] arr = {2, 6, 8, 7, 9, 4, 1};
        // 相邻的两个元素两两比较, 将较大的元素放到后边;
        for (int i = 0; i < arr.length - 1; i++) {// 外层循环, 控制比较的轮数
            for (int j = 0; j < arr.length - 1 - i; j++) {// 内层循环, 控制每一轮比较,
                if (arr[j] > arr[j + 1]) {// 比较相邻的两个元素,将较大的元素放到后边;
                    var temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        System.out.println(Arrays.toString(arr));// [1, 2, 4, 6, 7, 8, 9]
    }
}

Integer

1. 包装类

①: 作用

  • 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
  • 常用的操作之一:用于基本数据类型与字符串之间的转换

② 基本类型对应的包装类
在这里插入图片描述

2. 常用方法

  • public static Integer valueOf(String s) 返回一个保存指定值的 Integer 对象 String
public class IntegerDemo {
    public static void main(String[] args) {
        //public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String
        Integer i = Integer.valueOf("100");
        System.out.println(i);
    }
}

3. int和String类型的相互转换

① int转换为String

  • 方式一:直接在数字后加一个空字符串;
  • 方式二:通过String类静态方法valueOf();
public class IntegerDemo {
    public static void main(String[] args) {
        //int --- String
        int number = 100;
        //方式1
        String s1 = number + "";
        System.out.println(s1);
        System.out.println("--------");
        //方式2
        //public static String valueOf(int i)
        String s2 = String.valueOf(number);
        System.out.println(s2);
    }
}

② String转换为int

  • 方式一:先将字符串数字转成Integer,再调用valueOf()方法
  • 方式二:通过Integer静态方法parseInt()进行转换
public class IntegerDemo {
    public static void main(String[] args) {
        //String --- int
        String s = "100";
        //方式1:String --- Integer --- int
        Integer i = Integer.valueOf(s);
        //public int intValue()
        int x = i.intValue();
        System.out.println(x);
        //方式2
        //public static int parseInt(String s)
        int y = Integer.parseInt(s);
        System.out.println(y);
    }
}

4. 自动拆箱和自动装箱

① 自动装箱

  • 把基本数据类型转换为对应的包装类类型

② 自动拆箱

  • 把包装类类型转换为对应的基本数据类型

Date

1. 概述

  • Date 代表了一个特定的时间,精确到毫秒;

2. 构造方法

  • public Date()分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒;
  • public Date(long date)分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数;

3. 常用方法

  • public long getTime()获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
  • public void setTime(long time)设置时间,给的是毫秒值
public class DateDemo02 {
    public static void main(String[] args) {
        //创建日期对象
        Date d = new Date();

        //public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
        System.out.println(d.getTime());
        System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");

        //public void setTime(long time):设置时间,给的是毫秒值
//        long time = 1000*60*60;
        long time = System.currentTimeMillis();
        d.setTime(time);

        System.out.println(d);
    }
}

SimpleDateFormat

1. 概述

  • 用于以区域设置敏感的方式格式化和解析日期。

2. 构造方法

  • public SimpleDateFormat()构造一个SimpleDateFormat,使用默认模式和日期格式
  • public SimpleDateFormat(String pattern)构造一个SimpleDateFormat使用给定的模式和默认的日期格式

3. 常用方法

① 格式化(从Date到String)

  • public final String format(Date date):将日期格式化成日期/时间字符串

② 解析(从String到Date)

  • public Date parse(String source):从给定字符串的开始解析文本以生成日期
public class SimpleDateFormatDemo {
    public static void main(String[] args) throws ParseException {
        //格式化:从 Date 到 String
        Date d = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String s = sdf.format(d);
        System.out.println(s);
        System.out.println("--------");

        //从 String 到 Date
        String ss = "2048-08-09 11:11:11";
        //ParseException
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dd = sdf2.parse(ss);
        System.out.println(dd);
    }
}

Calendar

1. 概述

  • 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法;

2. 创建方式

  • Calendar rightNow = Calendar.getInstance();

3. 常用方法

  • public int get(int field)返回给定日历字段的值
  • public abstract void add(int field, int amount)根据日历的规则,将指定的时间量添加或减去给定的日历字段
  • public final void set(int year,int month,int date)设置当前日历的年月日
public class CalendarDemo {
    public static void main(String[] args) {
        //获取日历类对象
        Calendar c = Calendar.getInstance();

        //public int get(int field):返回给定日历字段的值
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");

        //public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段

        //需求:10年后的10天前
        c.add(Calendar.YEAR,10);
        c.add(Calendar.DATE,-10);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");

        //public final void set(int year,int month,int date):设置当前日历的年月日
        c.set(2050,10,10);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
    }
}
发布了22 篇原创文章 · 获赞 4 · 访问量 1280

猜你喜欢

转载自blog.csdn.net/weixin_42931689/article/details/104206670