Math类、Random类、System类

Math类

public class Demo{
  public static void main(String args[]){ 
    /** 
     *Math.sqrt()//计算平方根
     *Math.cbrt()//计算立方根
     *Math.pow(a, b)//计算a的b次方
     *Math.max( , );//计算最大值
     *Math.min( , );//计算最小值
     */
    System.out.println(Math.sqrt(16));  //4.0 
    System.out.println(Math.cbrt(8));  //2.0
    System.out.println(Math.pow(3,2));   //9.0
    System.out.println(Math.max(2.3,4.5));//4.5
    System.out.println(Math.min(2.3,4.5));//2.3
    /** 
     * abs求绝对值 
     */
    System.out.println(Math.abs(-10.4));  //10.4 
    System.out.println(Math.abs(10.1));   //10.1 
    /** 
     * ceil天花板的意思,就是返回大的值
     */
    System.out.println(Math.ceil(-10.1));  //-10.0 
    System.out.println(Math.ceil(10.7));  //11.0 
    System.out.println(Math.ceil(-0.7));  //-0.0 
    System.out.println(Math.ceil(0.0));   //0.0 
    System.out.println(Math.ceil(-0.0));  //-0.0 
    System.out.println(Math.ceil(-1.7));  //-1.0
    /** 
     * floor地板的意思,就是返回小的值 
     */
    System.out.println(Math.floor(-10.1)); //-11.0 
    System.out.println(Math.floor(10.7));  //10.0 
    System.out.println(Math.floor(-0.7));  //-1.0 
    System.out.println(Math.floor(0.0));  //0.0 
    System.out.println(Math.floor(-0.0));  //-0.0 
    /** 
     * random 取得一个大于或者等于0.0小于不等于1.0的随机数 
     */
    System.out.println(Math.random()); //小于1大于0的double类型的数
    System.out.println(Math.random()*2);//大于0小于1的double类型的数
    System.out.println(Math.random()*2+1);//大于1小于2的double类型的数
    /** 
     * rint 四舍五入,返回double值 
     * 注意.5的时候会取偶数  异常的尴尬=。=
     */
    System.out.println(Math.rint(10.1));  //10.0 
    System.out.println(Math.rint(10.7));  //11.0 
    System.out.println(Math.rint(11.5));  //12.0 
    System.out.println(Math.rint(10.5));  //10.0 
    System.out.println(Math.rint(10.51));  //11.0 
    System.out.println(Math.rint(-10.5));  //-10.0 
    System.out.println(Math.rint(-11.5));  //-12.0 
    System.out.println(Math.rint(-10.51)); //-11.0 
    System.out.println(Math.rint(-10.6));  //-11.0 
    System.out.println(Math.rint(-10.2));  //-10.0 
    /** 
     * round 四舍五入,float时返回int值,double时返回long值 
     */
    System.out.println(Math.round(10.1));  //10 
    System.out.println(Math.round(10.7));  //11 
    System.out.println(Math.round(10.5));  //11 
    System.out.println(Math.round(10.51)); //11 
    System.out.println(Math.round(-10.5)); //-10 
    System.out.println(Math.round(-10.51)); //-11 
    System.out.println(Math.round(-10.6)); //-11 
    System.out.println(Math.round(-10.2)); //-10 
  } 
}

Random类

/**
     * 测试Random类中的简单方法
     */
    @Test
    public void test02() {
        Random random = new Random();
        System.out.println("nextInt():" + random.nextInt());   //随机生成一个整数,这个整数的范围就是int类型的范围-2^31~2^31-1
        System.out.println("nextLong():" + random.nextLong());      //随机生成long类型范围的整数
        System.out.println("nextFloat():" + random.nextFloat());    //随机生成[0, 1.0)区间的小数
        System.out.println("nextDouble():" + random.nextDouble());  //随机生成[0, 1.0)区间的小数
       
        byte[] byteArr = new byte[10];
        random.nextBytes(byteArr);  //随机生成byte,并存放在定义的数组中,生成的个数等于定义的数组的个数
        for (int i = 0; i < byteArr.length; i++) {
            System.out.println(byteArr[i]);
        }
       
        /**
         * random.nextInt(n)
         * 随机生成一个正整数,整数范围[0,n)
         * 如果想生成其他范围的数据,可以在此基础上进行加减
         *
         * 例如:
         * 1. 想生成范围在[0,n]的整数
         *      random.nextInt(n+1)
         * 2. 想生成范围在[m,n]的整数, n > m
         *      random.nextInt(n-m+1) + m 
         *      random.nextInt() % (n-m) + m
         * 3. 想生成范围在(m,n)的整数
         *      random.nextInt(n-m+1) + m -1
         *      random.nextInt() % (n-m) + m - 1
         * ...... 主要是依靠简单的加减法  
         */
        System.out.println("nextInt(10):" + random.nextInt(10)); //随机生成一个整数,整数范围[0,10)
        for (int i = 0; i < 20; i++) {
            //[3,15)
            //这里有坑,需要注意,如果前面用了+号,应该要把计算结果整体用括号括起来,不然它会把+号解释为字符串拼接
            System.out.println("我生成了一个[3,15)区间的数,它是:" + (random.nextInt(12) + 3));
        }
    }

JDK1.8新增方法:

public class RandomTest {
    /**
     * 测试Random类中 JDK1.8提供的新方法
     * JDK1.8新增了Stream的概念
     * 在Random中,为double, int, long类型分别增加了对应的生成随机数的方法
     * 鉴于每种数据类型方法原理是一样的,所以,这里以int类型举例说明用法
     */
    @Test
    public void test03() {
        Random random = new Random();
        random.ints();  //生成无限个int类型范围内的数据,因为是无限个,这里就不打印了,会卡死的......
        random.ints(10, 100);   //生成无限个[10,100)范围内的数据
       
        /**
         * 这里的toArray 是Stream里提供的方法
         */
        int[] arr = random.ints(10).toArray();  //生成10个int范围类的个数。
        System.out.println(arr.length);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
       
       
        //生成5个在[10,100)范围内的整数
        random.ints(5, 10, 100).forEach(System.out :: println); //这句话和下面三句话功能相同
        //forEach等价于:
        arr = random.ints(5, 10, 100).toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
       
        /**
         * 对于
         *      random.ints();
         *      random.ints(ori, des);
         * 两个生成无限个随机数的方法,我们可以利用Stream里的terminal操作,来截断无限这个操作
         */
        //limit表示限制只要10个,等价于random.ints(10)
        random.ints().limit(10).forEach(System.out :: println);
       
        //等价于random.ints(5, 10, 100)
        random.ints(10, 100).limit(5).forEach(System.out :: println);
    }   
}

System类

提供静态方法,并可以获取与系统相关的信息或系统级操作.

  • public static long currentTimeMillis();获取当前时间的毫秒值.
    1秒 = 1000毫秒
    时间零点:1970.1.1(起点)(系统自动匹配当前系统的时区)
    作用:
    *性能测试(记录开始时间start,消耗时间 = 结束时间 - start)
public void run(){
    //记录开始时间
    long start = System.currentTimeMillis();
    for(int i = 0;i<100000;i++){
    }
    //获得执行时间
    long time = System.currentTimeMillis() - start;
}
  • public static void gc():通知垃圾回收器回收垃圾对象(没有指向的对象)
public class SystemDemo {
    public static void main(String[] args){
         for(int i = 0; i < 10;i++){
            //创建Person对象
            Person p = new Person();
            //调用gc方法,可以观察到输出的"被回收了吗?"并没有10个
            System.gc();
         }
    }
}
 
class Person{
 
    @Override
    protected void finalize() throws Throwable{
         System.out.println("被回收了吗?");
    }
}

*finalize()是Object类的方法,该方法是有JVM自动调用的,当该对象被垃圾回收器回收的时候,JVM会自动调用该方法,也可能不调用.
System.gc()只是起到一个通知垃圾回收器的作用.

  • public staitic void exit():退出jvm,终止程序运行.
    System.exit(0);//正常终止
     
    System.exit(-1);//异常终止

猜你喜欢

转载自blog.csdn.net/ly823260355/article/details/83585659