Math类
package com.heima.otherclass;
public class Demo1_Math {
/**
* @param args
* * A:Math类概述
* Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
* B:成员方法
* 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) min自学
* public static double pow(double a,double b)
* public static double random()
* public static int round(float a) 参数为double的自学,四舍五入
* public static double sqrt(double a)
*/
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.abs(-10)); //取绝对值
//ceil天花板
/*
* 13.0
* 12.3
* 12.0
*/
System.out.println(Math.ceil(12.3)); //向上取整,但是结果是一个double
System.out.println(Math.ceil(12.99));
System.out.println("-----------");
//floor地板
/*
* 13.0
* 12.3
* 12.0
*/
System.out.println(Math.floor(12.3)); //向下取整,但是结果是一个double
System.out.println(Math.floor(12.99));
//获取两个值中的最大值
System.out.println(Math.max(20, 30));
//前面的数是底数,后面的数是指数
System.out.println(Math.pow(2, 3)); //2.0 ^ 3.0
//生成0.0到1.0之间的所以小数,包括0.0,不包括1.0
System.out.println(Math.random());
//四舍五入
System.out.println(Math.round(12.3f));
System.out.println(Math.round(12.9f));
//开平方
System.out.println(Math.sqrt(4));
System.out.println(Math.sqrt(2));
System.out.println(Math.sqrt(3));
}
}
Random类
package com.heima.otherclass;
import java.util.Random;
public class Demo2_Random {
/**
* * A:Random类的概述
* 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
* 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
* B:构造方法
* public Random()
* public Random(long seed)
* C:成员方法
* public int nextInt()
* public int nextInt(int n)(重点掌握) 0到n-1之间的随机数。
*/
public static void main(String[] args) {
Random r = new Random();
/*int x = r.nextInt();
System.out.println(x);*/
for(int i = 0; i < 10; i++) {
//System.out.println(r.nextInt());
System.out.println(r.nextInt(100)); //要求掌握,生成在0到n范围内的随机数,包含0不包含n
}
/*
* -1244746321
1060493871
-1244746321
1060493871
*/
/*Random r2 = new Random(1001);
// 给参数种子后,运行2次结果都是一样的,但是重复运算结果还是不同。
int a = r2.nextInt();
int b = r2.nextInt();
System.out.println(a);
System.out.println(b);*/
}
}
System类
大部分方法都是静态的
私有了构造方法,无法创建对象
System.in
System.out
package com.heima.otherclass;
public class Demo3_System {
/**
* * A:System类的概述
* System 类包含一些有用的类字段和方法。它不能被实例化。
* B:成员方法
* public static void gc() 运行垃圾回收器
* public static void exit(int status) 终止当前运行的Java虚拟器,参数:非0为异常终止,
* public static long currentTimeMillis() 当前时间毫秒值,可以算程序运行时间
* pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* C:案例演示
* System类的成员方法使用
*/
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
int[] src = {11,22,33,44,55};
int[] dest = new int[8];
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
System.out.println("--------------------------");
System.arraycopy(src, 0, dest, 0, src.length); //将数组内容拷贝
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
}
public static void demo3() {
long start = System.currentTimeMillis(); //1秒等于1000毫秒
for(int i = 0; i < 1000; i++) {
System.out.println("*");
}
long end = System.currentTimeMillis(); //获取当前时间的毫秒值
System.out.println(end - start);
}
public static void demo2() {
System.exit(1); //非0状态是异常终止,退出jvm
System.out.println("11111111111"); // 不被执行
}
public static void demo1() {
for(int i = 0; i < 100; i++) {
new Demo();
System.gc(); //运行垃圾回收器,相当于呼喊保洁阿姨,相当于自动调用Object类中的finalize方法
}
}
}
class Demo { //在一个源文件中不允许定义两个用public修饰的类
@Override
public void finalize() {
System.out.println("垃圾被清扫了");
}
}