Loading

常用API(Math,System,Object,Arrays)

Math


        // 绝对值
        System.out.println(Math.abs(-88));  // 88

        // 向上取整
        System.out.println(Math.ceil(12.34));   // 13.0
        System.out.println(Math.ceil(12.56));   // 13.0

        // 向下取整
        System.out.println(Math.floor(12.34));  // 12.0
        System.out.println(Math.floor(12.56));  // 12.0

        // 四舍五入 public static int round(float a);
        System.out.println(Math.round(12.34F)); // 12
        System.out.println(Math.round(12.56F)); // 13

        // public static int max(int a,int b)
        System.out.println(Math.max(66,88));    // 88


        // public static double pow(double a,double b);
        // public static double random(); 返回[0.0,1.0)

System


  public static void exit(int status);      // 终止当前运行的虚拟机,非0表示异常终止
  public static long currentTimeMillis();   // 返回当前时间(以 毫秒 为单位)
        System.out.println("start");
//        System.exit(0);         // 后面的程序 都不会再执行了
        System.out.println("end");

        long start = System.currentTimeMillis();
        for(int i = 0; i<100000; i++){
            System.out.println(i);
        }
        long end = System.currentTimeMillis();

        System.out.println("共耗时:" + (end - start) + "毫秒");

Obiect


  public String toString();              // 返回对象的字符串表示形式
  public boolean equals(Obiect obj);      // 比较对象地址是否相等
        PingPangPlayer p1 = new PingPangPlayer();

        // println 方法中 使用了 toString()
        System.out.println(p1);                    // com.itheima.PingPangPlayer@1d81eb93
        System.out.println(p1.toString());         // com.itheima.PingPangPlayer@1d81eb93

Arrays


import java.util.Arrays;
Arrays 类包含用于 操作数组 的各种方法。

public static String toString(int [] a);    // 返回指定数组内容的字符串表示形式
public static void sort(int [] a);          // 对数组进行排序
        int [] arr = {24,69,80,57,13};
        System.out.println("排序前:" + Arrays.toString(arr));
        // 排序前:[24, 69, 80, 57, 13]
        Arrays.sort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));
        // 排序后:[13, 24, 57, 69, 80]

工具类的设计思想:
构造方法用 private 修饰,防止外界创建对象。
成员方法用 public static 修饰,调用的时候 ,使用类名进行调用。

posted @ 2022-03-27 15:34  h/hx  阅读(87)  评论(0)    收藏  举报