参考笔记:java Math类 和 System类 详解(通俗易懂)-CSDN博客
目录
① static void exit(int status):
② static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):
③ static long currentTimeMillis() :
1.Math类
1.1 简介
Java 的 Math 类中封装了常用的数学运算,包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数
Math 类位于 java.lang.Math,其源码如下:
可以看到,Math 类由 final 关键字修饰, 所以 Math 不可被子类继承。其构造器被设置为 private ,所以无法在外部创建其对象实例
1.2 常用方法
① Math.E、Math.PI :自然对数 e 、圆周率
② static type abs(type a):返回 a 的绝对值,可以是 int、long、float、double 四种类型,对应的包装类型也可以
③ static double pow(double a, double b):返回
④ static double ceil(double a):对 a 作向上取整,返回大于等于 a 的最小整数
⑤ static double floor(double a):对 a 作向下取整,返回小于等于 a 的最大整数
⑥ static int/long round(float/double a):四舍五入取整,支持传入 float、double,对应的返回值类型为 int、long
⑦ static double sqrt(double a):返回
⑧ static double random():返回区间 [0.0, 1.0) 内的一个随机数
⑨ static type max (type a,type b):返回两数中的最大值,两数类型需要一致, type 支持 int、long、float、double 及其对应的包装类类型
⑩ static type min (type a,type b):返回两数中的最小值,两数类型需要一致,type 支持 int、long、float、double 及其对应的包装类类型
1.3 使用案例
public class demo {
public static void main(String[] args) {
//演示 : Math类常用方法
//1 —— Math.E、Math.PI
System.out.println("e:"+Math.E);
System.out.println("圆周率:"+Math.PI);
//2 —— static type abs(type a)
System.out.println("-233的绝对值 = " + Math.abs(-233));
System.out.println("6.666的绝对值 = " + Math.abs(6.666));
System.out.println("6.666的绝对值 = " + Math.abs(-11.11));
System.out.println("6.666的绝对值 = " + Math.abs(5));
System.out.println("===============================");
//3 —— static double pow(double a, double b)
System.out.println("3 的 2次方 = " + Math.pow(3, 2));
System.out.println("2 的 3次方 = " + Math.pow(2, 3));
System.out.println("4 的 2次方 = " + Math.pow(4, 2));
System.out.println("2 的 10次方 = " + Math.pow(2, 10));
System.out.println("===============================");
//4 —— static double ceil(double a)
System.out.println("2.33向上取整 = " + Math.ceil(2.33));
System.out.println("2.99向上取整 = " + Math.ceil(2.99));
System.out.println("3.01向上取整 = " + Math.ceil(3.01));
System.out.println("-3.01向上取整 = " + Math.ceil(-3.01));
System.out.println("===============================");
//5 —— static double floor(double a)
System.out.println("2.33向下取整 = " + Math.floor(2.33));
System.out.println("2.99向下取整 = " + Math.floor(2.99));
System.out.println("4.01向下取整 = " + Math.floor(4.01));
System.out.println("-4.01向下取整 = " + Math.floor(-4.01));
System.out.println("===============================");
//6 —— static int/long round(float/double a)
System.out.println("2.499四舍五入 = " + Math.round(2.499));
System.out.println("2.501四舍五入 = " + Math.round(2.501));
System.out.println("-3.33四舍五入 = " + Math.round(-3.33));
System.out.println("-6.88四舍五入 = " + Math.round(-6.88));
System.out.println("===============================");
//7 —— static double sqrt(double a)
System.out.println("9开根号 = " + Math.sqrt(9));
System.out.println("9.0开根号 = " + Math.sqrt(9.0));
System.out.println("1.21开根号 = " + Math.sqrt(1.21));
System.out.println("256开根号 = " + Math.sqrt(256));
System.out.println("===============================");
//8 —— static double random()
System.out.println("返回[0.0, 1.0)区间内的一个随机数:" + Math.random());
System.out.println("返回[0.0, 1.0)区间内的一个随机数:" + Math.random());
System.out.println("返回一个2~11间的随机整数 = " + (int) (2 + Math.random() * (11 - 2 + 1)));
System.out.println("返回一个2~11间的随机整数 = " + (int) (2 + Math.random() * (11 - 2 + 1)));
System.out.println("===============================");
//9 —— static ... max(..., ...) PS:支持double,float,int,long类型
System.out.println("3.2 和 2.3中的最大值 = " + Math.max(3.2, 2.3));
System.out.println("-2.01 和 -1.99中的最大值 = " + Math.max(-2.01, -1.99));
System.out.println("2333 和 3333中的最大值 = " + Math.max(2333, 3333));
System.out.println("-666 和 11中的最大值 = " + Math.max(-666, 11));
System.out.println("===============================");
//10 —— static ... min(..., ...) PS:支持double,float,int,long类型
System.out.println("3.2 和 2.3中的最小值 = " + Math.min(3.2, 2.3));
System.out.println("-2.01 和 -1.99中的最小值 = " + Math.min(-2.01, -1.99));
System.out.println("2333 和 3333中的最小值 = " + Math.min(2333, 3333));
System.out.println("-666 和 11中的最小值 = " + Math.min(-666, 11));
}
}
2.System类
2.1 简介
System 类位于 java.lang.System,其源码如下:
同样,由于用 fianl 关键字修饰,System 类不可被继承;构造器被设置为 private,因此不可被实例化
2.2 常用方法及案例
① static void exit(int status):
退出当前程序,结束正在运行的 java 虚拟机。status = 0 表示以正常状态退出
案例
public class demo {
public static void main(String[] args) {
//演示 : System类常用方法
//1.exit()
System.out.println("蔡徐坤 yyds!");
System.exit(0);
System.out.println("这句话能否输出?");
}
}
② static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):
该方法可以用于数组的拷贝,可以将原数组中的指定内容拷贝到指定新数组中的指定位置
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
注:源码中该方法用 native 关键字修饰,底层是用 C/C++ 实现的,所以看不到方法体
-
平时拷贝数组更多使用的是 Arrays 类中的 copyOf ( ) ,实际上 copyOf( ) 在底层调用的就是 System 类中的 arraycopy
-
参数解释
-
src:原数组
-
srcPos:要从原数组的哪个位置(索引)处开始拷贝
-
dest:目标数组
-
destPost:从目标数组的哪个位置处(索引)开始存放拷贝内容
-
length:从原数组中拷贝的内容的长度
-
简单来说该方法实现的效果就是:把 src 数组中从 srcPos 索引开始共 length 个元素拷贝到 dest 数组中的从 destPos 索引开始处
实际上,往往使用 "System.arraycopy(arr1,0,arr2,0,arr1.length())" 的格式,即将 arr1 数组的所有内容拷贝至新数组 arr2 中
案例
import java.util.Arrays;
public class demo {
public static void main(String[] args) {
//演示 : System类常用方法
//2.arraycopy()
int[] array1 = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] array2 = new int[array1.length];
System.arraycopy(array1, 0, array2, 0, array1.length);
System.out.println("拷贝后的新数组array2 = " + Arrays.toString(array2));
int[] array3 = new int[25];
System.arraycopy(array1, 0, array3, 11, 10);
System.out.println("拷贝后的新数组array3 = " + Arrays.toString(array3));
int[] array4 = new int[5];
System.arraycopy(array1, 2, array4, 0, 3);
System.out.println("拷贝后的新数组array4 = " + Arrays.toString(array4));
}
}
③ static long currentTimeMillis() :
返回当前的时间点离 1970年1月1日午夜12:00 时间点(1970-1-1-00:00:00) 的毫秒数。至于为什么是 1970-1-1 的原因:
实际开发中,一般用来计算某段程序或某个函数的执行时间
案例
import java.util.Arrays;
public class demo {
public static void main(String[] args) {
//演示 : System类常用方法
//3.currentTimeMillis()
Long time_Start = System.currentTimeMillis();
System.out.println("当前时间距离1970-1-1-00:00:00的毫秒数 = " + time_Start);
for (int i = 0; i < 10000000; i++) {
System.out.print("牛逼 ");
}
Long time_End = System.currentTimeMillis();
System.out.println("\n执行完牛逼for循环用了多少毫秒?" + (time_End - time_Start));
}
}