2019-06-17 java学习日记

Math类的概述

Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

Math类的成员方法

public static int abs(int a)

public static double ceil(double a)

public static double floor(double a)

public static int max(int a,int b)

public static double pow(double a,double b)

public static random()

public static int round(float a) 

public static double sqrt(double a)

 1   public static void main(String[] args) {
 2     System.out.println(Math.PI);
 3     System.out.println(Math.abs(-10)); //取绝对值
 4     
 5     //ceil天花板,获取向上取整,但是结果是一个double
 6     System.out.println(Math.ceil(12.3));
 7     
 8     //向下取整
 9     System.out.println(Math.floor(12.3));
10     
11     //获取两个值中的最大值
12     System.out.println(Math.max(10, 20));
13     
14     //前面的数是底数,后面的数是指数
15     System.out.println(Math.pow(2, 3)); //2.0^3.0=8
16     
17     //生成0.0到1.0之间的所以小数
18     System.out.println(Math.random());
19     
20     //四舍五入
21     System.out.println(Math.round(12.5f));
22     
23     //开平方
24     System.out.println(Math.sqrt(3));
25     }

Random类的概述

Random类的概述此类用于产生随机数如果用相同的种子创建两个 Random 实例,
则对每个实例进行相同的方法调用序列,他们将生成并返回相同的数字序列

构造方法

public Random()
public Random(long seed)

成员方法

public int nexInt()
public int nextInt(int n)

 1  public static void main(String[] args) {
 2         Random r1 = new Random();
 3         for (int i = 0; i < 5; i++) {                                        
 4             System.out.println(r1.nextInt(100));        
 5             
 6         }
 7         Random r2 = new Random(1000);
 8         int i = r2.nextInt();
 9         int j = r2.nextInt();
10         System.out.println(i);
11         System.out.println(j);
12     }

System类的概述

System类包含一些有用的类字段和方法。它不能被实例化

成员方法

public static void gc()

public static void exit(int status)

public static long currentTimeMillis()

public static void arraycopy(Object src, int srcPos,Object dest, int destPos,int length)

 public static void main(String[] args) {
        //demo1();    
        //demo2();
        //demo3();
        int [] arr={1,5,3,8};
        int [] dest=new int[8];
        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }
        System.arraycopy(arr, 0, dest, 0, arr.length);
        for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }
        
    }

    public static void demo3() {
        long start=System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            System.out.println("*");
        }
    //获取当前的毫秒值
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }

    public static void demo2() {
     //退出java虚拟机
        System.exit(0);
        System.out.println("11111111");
    }

    public static void demo1() {
        for (int i = 0; i < 10; i++) {
        new demo();    
     //运行垃圾收回器
        System.gc(); 
        }
    }
}
    //在一个源文件中,是不允许定义两个public修饰类
class demo{

    @Override
    public void finalize(){
        System.out.println("垃圾被清扫了");
        
    }    
原文地址:https://www.cnblogs.com/Sherwin-liao/p/11048532.html