常见API(Object类,包装类,StringBuilder,StringBuffer,StringJoiner,Math,BigDecimal,传统日期时间)

目录

1.API

 2.object类:

2.Objects:

3.包装类

 4..StringBuilder 

5.案例:返回任意整型数组的内容

6.StringJoiner

7.Math

 8.System

9. Runtime

10.BigDecimal

11.传统的日期,时间

Date

12.SimpleDateFormat


1.API

应用程序编程接口,就是java帮我们已经写好一些程序,如:类、方法等,我们直接拿过来用就可以解决一些问题。

 2.object类:

Object类是Java中所有类的祖宗类,因此,Java中所有类的对象都可以直接使用Object类中提供的一些方法。

toString存在的意义:toString()方法存在的意义就是为了被子类重写,以便返回对象具体的内容。

equals存在的意义:直接比较两个对象的地址是否相同完全可以用“==”替代equals,equals存在的意义就是为了被子类重写,以便子类自己来定制比较规则(比如比较对象内容)。 

//Student类:
public class Student { // extends Object{
    private String name;
    private int age;

右击——Generate——toString
@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }



//重写equals方法,比较两个对象的内容一样返回true
//比较者:s2 ==this  比较者 s1 == o
    @Override
    public boolean equals(Object o) {
 //1.判断两个对象是否地址一样,一样直接返回TRUE
        if (this == o) return true;

//判断o是null直接返回false ,或者比较者的类型与被比较者的类型不一样,返回false
        if (o == null || getClass() != o.getClass()) return false;
        
//3.o不是null,且o一定是学生类型的对象,开始比较内容了
        Student student = (Student) o;
        return this.age == student.age && Objects.equals(this.name, student.name);
    }




Test类:
Student s1 =new Student("乐乐",23);
       // System.out.println(s1.toString());
  System.out.println(s1);

 Student s2 =new Student("乐乐",23);
        System.out.println(s2==s1);  //false  
        System.out.println(s2.equals(s1));  //false  //都是比较地址




  clone() 对象克隆 :  

当某个对象调用这个方法时,这个方法会复制一个一模一样的新对象返回

1.先学生类中clone,回车 ,然后类标题后加implements Clone

2.s.clone,(alt+回车),User  u2 = (Ueser) u1.clone();  u1.get().sout

2.Objects:

一个工具类,提供很多操作对象的静态方法给我们使用

 s1.equals(s2)  字符串的equals判断对象内容是否相同

         String s1= null;
         String s2 = "itheima";

        System.out.println(s1.equals(s2));  //空指针异常
        System.out.println(Objects.equals(s1, s2));  //false  更安全,准确 

        System.out.println(Objects.isNull(s1));//true
        System.out.println(s1==null);//true
        System.out.println(Objects.isNull(s2));//false
        System.out.println(s2 == null);//false

        System.out.println(Objects.nonNull(s2));//true
        System.out.println(Objects.nonNull(s1));//false

3.包装类

基本类型的数据包装成对象           

int——Integer       

char——Character


//掌握包装类的使用


Integer a2 = Integer.valueOf(12);
        System.out.println(a2);  //12

        //自动装箱,可以自动把基本类型的数据转换成对象
        Integer a3 =12;

        //自动拆箱,可以自动把包装类型的对象转换成对应的基本数据类型
        int a4 = a3;

        //泛型和集合不支持基本数据类型,只能支持引用数据类型
        ArrayList<Integer> list = new ArrayList<>();
        list.add(12);//自动装箱
        list.add(13);

        int rs = list.get(1);//自动拆箱
        System.out.println("====================");

         //基本类型的数据转换成字符串
        Integer a =23;
        String rs1 = Integer.toString(a);
        System.out.println(rs1 + 1);//231

        String rs2 = a.toString();  //"23"
        System.out.println(rs2 + 1);//231

        String rs3 = a + "";
        System.out.println(rs3 + 1);  //231

       //2.把字符串类型的数值转换成对应的基本类型
        String ageStr = "29";
        int ageI = Integer.valueOf(ageStr); //29
        System.out.println(ageI + 1);  //30

        String scoreStr = "99.5";
        Double score =Double.valueOf(scoreStr); //99.5
        System.out.println(score + 0.5);

 4..StringBuilder 

代表可变字符串对象,相当于一个容器,里面装的字符串可以改变的,就是用来操作字符串的

好处:StringBuilder比String更适合做字符串的修改操作

注意:

如果操作字符串较少,或者不需要操作,以及定义字符串变量,还是建议用String

对于字符串相关的操作,如频繁的拼接、修改等,建议用StringBuidler,效率更高!

           StringBuilder跟StringBuffer一模一样

            StringBuilder线程不安全,StringBuffer线程安全

5.案例:返回任意整型数组的内容

格式[11,22,66]

public class Test {
    public static void main(String[] args) {
        System.out.println(getArrayData(new int[]{11, 22, 66}));
    }

    public static String getArrayData(int[] arr) {
        //1.判断arr是否为null
        if (arr == null) {
            return null;
        }
        //2.arr数组对象存在 arr = [11,22,66]
       StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i==arr.length-1){
                sb.append(arr[i]);
            }else {
                sb.append(arr[i]).append(",");
            }
        }
        sb.append("]");

         return sb.toString();
    }

6.StringJoiner

JDK8开始才有的,跟StringBuilder一样,也是用来操作字符串的,也可以看成是一个容器,创建之后里面的内容是可变的。代码更简洁

 public static void main(String[] args) {
        StringJoiner s = new StringJoiner(", ", "[", "]");  //间隔符
        s.add("java1");
        s.add("java2");
        s.add("java3");
        System.out.println(s); //java1,java2,java3

    }


//案例
    public static String getArrayData(int[] arr) {
        //1.判断arr是否为null
        if (arr == null) {
            return null;
        }
        //2.arr数组对象存在 arr = [11,22,66]
        StringJoiner s = new StringJoiner(", ", "[", "]");
        for (int i = 0; i < arr.length; i++) {
            s.add(arr[i] + "");
        }
        return s.toString();
    }

7.Math

代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法

// 目标:了解下Math类提供的常见方法。
     // 1、public static int abs(int a):取绝对值(拿到的结果一定是正数)
     //    public static double abs(double a)
        System.out.println(Math.abs(-12)); // 12
        System.out.println(Math.abs(123)); // 123
        System.out.println(Math.abs(-3.14)); // 3.14

        // 2、public static double ceil(double a): 向上取整
        System.out.println(Math.ceil(4.0000001)); // 5.0
        System.out.println(Math.ceil(4.0)); // 4.0

        // 3、public static double floor(double a): 向下取整
        System.out.println(Math.floor(4.999999)); // 4.0
        System.out.println(Math.floor(4.0)); // 4.0

        // 4、public static long round(double a):四舍五入
        System.out.println(Math.round(3.4999)); // 3
        System.out.println(Math.round(3.50001)); // 4

        // 5、public static int max(int a, int b):取较大值
        //   public static int min(int a, int b):取较小值
        System.out.println(Math.max(10, 20)); // 20
        System.out.println(Math.min(10, 20)); // 10

        // 6、 public static double pow(double a, double b):取次方
        System.out.println(Math.pow(2, 3)); // 2的3次方   8.0
        System.out.println(Math.pow(3, 2)); // 3的2次方   9.0

        // 7、public static double random(): 取随机数 [0.0 , 1.0) (包前不包后)
        System.out.println(Math.random());
    }
}

 8.System

System代表程序所在的系统,也是一个工具类。

/**
 * 目标:了解下System类的常见方法。
 */
public class SystemTest {
    public static void main(String[] args) {

        // 1、public static void exit(int status):
        //   终止当前运行的Java虚拟机。
        //   该参数用作状态代码; 按照惯例,非零状态代码表示异常终止。
         System.exit(0); // 人为的终止虚拟机。(不要使用)

        // 2、public static long currentTimeMillis():
        //    获取当前系统的时间
        //    返回的是long类型的时间毫秒值
:指的是从(C语言的生日)1970-1-1 0:0:0开始走到此刻的总的毫秒值,1s = 1000ms
        long time = System.currentTimeMillis();
        System.out.println(time);

        for (int i = 0; i < 1000000; i++) {
            System.out.println("输出了:" + i);
        }

        long time2 = System.currentTimeMillis();
        System.out.println((time2 - time) / 1000.0 + "s");
    }
}

9. Runtime

代表程序所在的运行环境。

Runtime是一个单例类 。

public class RuntimeTest {
    public static void main(String[] args) throws IOException, InterruptedException {

        // 1、public static Runtime getRuntime() 返回与当前Java应用程序关联的运行时对象。
        Runtime r = Runtime.getRuntime();

        // 2、public void exit(int status) 终止当前运行的虚拟机,该参数用作状态代码; 按照惯例,非零状态代码表示异常终止。
        // r.exit(0);

        // 3、public int availableProcessors(): 获取虚拟机能够使用的处理器数。
        System.out.println(r.availableProcessors());

        // 4、public long totalMemory() 返回Java虚拟机中的内存总量。
        System.out.println(r.totalMemory()/1024.0/1024.0 + "MB"); // 1024 = 1K     1024 * 1024 = 1M

        // 5、public long freeMemory() 返回Java虚拟机中的可用内存量
        System.out.println(r.freeMemory()/1024.0/1024.0 + "MB");

        // 6、public Process exec(String command) 启动某个程序,并返回代表该程序的对象。
        // r.exec("D:\\soft\\XMind\\XMind.exe");
        Process p = r.exec("QQ");
        Thread.sleep(5000); // 让程序在这里暂停5s后继续往下走!!
        p.destroy(); // 销毁!关闭程序!
    }
}

10.BigDecimal

用于解决浮点型运算时,出现结果失真的问题

public class Test2 {
    public static void main(String[] args) {
        // 目标:掌握BigDecimal进行精确运算的方案。
        double a = 0.1;
        double b = 0.2;

        // 1、把浮点型数据封装成BigDecimal对象,再来参与运算。
        // a、public BigDecimal(double val) 得到的BigDecimal对象是无法精确计算浮点型数据的。 注意:不推荐使用这个,
        // b、public BigDecimal(String val)  得到的BigDecimal对象是可以精确计算浮点型数据的。 可以使用。
        // c、public static BigDecimal valueOf(double val): 通过这个静态方法得到的BigDecimal对象是可以精确运算的。是最好的方案。
       
        //把小数转换成字符串再得到BigDecimal对象来使用
        BigDecimal a1 = BigDecimal.valueOf(a);
        BigDecimal b1 = BigDecimal.valueOf(b);

        // 2、public BigDecimal add(BigDecimal augend): 加法
        BigDecimal c1 = a1.add(b1);
        System.out.println(c1);

        // 3、public BigDecimal subtract(BigDecimal augend): 减法
        BigDecimal c2 = a1.subtract(b1);
        System.out.println(c2);

        // 4、public BigDecimal multiply(BigDecimal augend): 乘法
        BigDecimal c3 = a1.multiply(b1);
        System.out.println(c3);

        // 5、public BigDecimal divide(BigDecimal b): 除法
        BigDecimal c4 = a1.divide(b1);
        System.out.println(c4);

//        BigDecimal d1 = BigDecimal.valueOf(0.1);
//        BigDecimal d2 = BigDecimal.valueOf(0.3);
//        BigDecimal d3 = d1.divide(d2);
//        System.out.println(d3);

        // 6、public BigDecimal divide(另一个BigDecimal对象,精确几位,舍入模式) : 除法,可以设置精确几位。
        BigDecimal d1 = BigDecimal.valueOf(0.1);
        BigDecimal d2 = BigDecimal.valueOf(0.3);
        BigDecimal d3 = d1.divide(d2,  2, RoundingMode.HALF_UP); // 0.33
        System.out.println(d3);

        // 7、public double doubleValue() : 把BigDecimal对象又转换成double类型的数据。
        //print(d3);
        //print(c1);
        double db1 = d3.doubleValue();
        double db2 = c1.doubleValue();
        print(db1);
        print(db2);
    }

    public static void print(double a){
        System.out.println(a);
    }
}

11.传统的日期,时间

Date

代表的是日期和时间

// 目标:掌握Date日期类的使用。
        // 1、创建一个Date的对象:代表系统当前时间信息的。
        Date d = new Date();
        System.out.println(d);

        // 2、拿到时间毫秒值。
        long time = d.getTime();
        System.out.println(time);

        // 3、把时间毫秒值转换成日期对象: 2s之后的时间是多少。
        time += 2 * 1000;
        Date d2 = new Date(time);
        System.out.println(d2);

        // 4、直接把日期对象的时间通过setTime方法进行修改
        Date d3 = new Date();
        d3.setTime(time);
        System.out.println(d3);

12.SimpleDateFormat

代表简单日期格式化,可以用来把日期对象、时间毫秒值格式化成我们想要的形式,可以把字符串的时间形式解析成 Date 日期对象

   // 目标:掌握SimpleDateFormat的使用。
        // 1、准备一些时间
        Date d = new Date();
        System.out.println(d);

        long time = d.getTime();
        System.out.println(time);

        // 2、格式化日期对象,和时间 毫秒值。
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");

        String rs = sdf.format(d);
        String rs2 = sdf.format(time);
        System.out.println(rs);
        System.out.println(rs2);
        System.out.println("-------------------------------------------------------------------");

        // 目标:掌握SimpleDateFormat解析字符串时间 成为日期对象。
        String dateStr = "2022-12-12 12:12:11";
        // 1、创建简单日期格式化对象 , 指定的时间格式必须与被解析的时间格式一模一样,否则程序会出bug.
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d2 = sdf2.parse(dateStr);
        System.out.println(d2);

Calendar

代表的是系统此刻时间对应的日历。

通过它可以单独获取、修改时间中的年、月、日、时、分、秒等。

 // 目标:掌握Calendar的使用和特点。
        // 1、得到系统此刻时间对应的日历对象。
        Calendar now = Calendar.getInstance();
        System.out.println(now);

        // 2、获取日历中的某个信息
        int year = now.get(Calendar.YEAR);
        System.out.println(year);

        int days = now.get(Calendar.DAY_OF_YEAR);
        System.out.println(days);

        // 3、拿到日历中记录的日期对象。
        Date d = now.getTime();
        System.out.println(d);

        // 4、拿到时间毫秒值
        long time = now.getTimeInMillis();
        System.out.println(time);

        // 5、修改日历中的某个信息
        now.set(Calendar.MONTH, 9); // 修改月份成为10月份。
        now.set(Calendar.DAY_OF_YEAR, 125); // 修改成一年中的第125天。
        System.out.println(now);

        // 6、为某个信息增加或者减少多少
        now.add(Calendar.DAY_OF_YEAR, 100);
        now.add(Calendar.DAY_OF_YEAR, -10);
        now.add(Calendar.DAY_OF_MONTH, 6);
        now.add(Calendar.HOUR, 12);
        now.set(2026, 11, 22);
        System.out.println(now);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值