目录
1.6 Math 类中的 pow(double a , double b) 方法
2.2 System 类中的 currentTimeMillis() 方法
3.1 Runtime 类中的 getRuntime() 方法
1 Math 类的说明
`Math`类是Java中的一个数学工具类,提供了各种数学运算的静态方法。它包含了计算绝对值、最大值、最小值、平方根、乘方、向上取整、向下取整、四舍五入和生成随机数等功能。除此之外,还提供了一些常用的数学函数和常量,比如三角函数、指数函数和π等。要使用这些方法,只需在代码中调用类名和方法名即可。
1.1 Math 类中的 abs() 方法
将传入的参数值转换为绝对值,传入的参数类型可以是 int ,double ,long,float 类型。
@Test
void mathAshTest(){
int a = Math.abs(-10);
double b = Math.abs(-10.10);
long c = Math.abs(-13129L);
float d = Math.abs(12.5F);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
// 10
// 10.1
// 13129
// 12.5
}
1.2 Math 类中的 ceil () 方法
向上取整方法,返回的是浮点型值。
@Test
void mathCeilTest(){
//对参数进行向上取整
double ceil = Math.ceil(198.99);
System.out.println(ceil);
// 199.0
}
1.3 Math 类中的 floor () 方法
向下取整方法,返回的是浮点型值。
@Test
void mathFloorTest(){
//对参数进行向下取整
double floor = Math.floor(198.99);
System.out.println(floor);
// 198.0
}
1.4 Math 类中的 round () 方法
四舍五入方法,传入的参数是浮点型,返回出来的是整型或长整型。
@Test
void mathRoundTest(){
long round = Math.round(3.1415926);
System.out.println(round); //3
}
1.5 Math 类中的 max() 和 min() 方法
比较方法,max返回两个数的最大值,min放回两个数的最小值
@Test
void mathMaxTest(){
double a = 3.14;
double b = 3.1415;
double max = Math.max(a, b);
System.out.println(max); //3.1415
}
@Test
void mathMinTest(){
double a = 3.14;
double b = 3.1415;
double min = Math.min(a, b);
System.out.println(min); //3.14
}
1.6 Math 类中的 pow(double a , double b) 方法
幂函数方法,返回a的b次幂的值
@Test
void mathPowTest(){
double pow = Math.pow(2, 2);
System.out.println(pow); //4.0
}
1.7 Math 类中的 random() 方法
返回一个随机数
@Test
void mathRandomTest(){
double min = 1;
double max = 100; // 注意这里是100而不是99,因为Math.random()生成的值范围是[0,1)
//生成10次
for (int i = 0; i < 11; i++) {
double random = min + Math.random() * (max - min);
System.out.println("第" +i+"次随机数"+ random);
// 第0次随机数10.69608803095157
// 第1次随机数62.54761399960396
// 第2次随机数7.57509544935212
// 第3次随机数2.8386781881499004
// 第4次随机数86.13331884015312
// 第5次随机数39.17728784225826
// 第6次随机数63.559880950290335
// 第7次随机数16.453389173356303
// 第8次随机数27.964169049090927
// 第9次随机数24.536525463830397
// 第10次随机数85.22118293750059
}
}
2 System 类的说明
System类是Java核心类库中的一个重要类,提供了与系统交互的方法和属性。
2.1 System 类中的 exit() 方法
终止当前正在执行的jvm虚拟机
@Test
void systemExitTest(){
System.exit(0);
}
2.2 System 类中的 currentTimeMillis() 方法
用于返回当前系统的时间(毫秒级),可用于做记录日志执行时间
@Test
void systemCurrentTimeMillisTest(){
long a = System.currentTimeMillis();
System.out.println(a);
for (int i = 0; i < 1000000; i++) {
}
long b = System.currentTimeMillis();
System.out.println(b);
System.out.println(b-a+"程序执行时间");
// 1713496634827
// 1713496634829
// 2程序执行时间
}
3 Runtime 类的说明
Runtime 类是 Java 中一个非常重要的类,它提供了与运行时环境的交互方式。Runtime 类的实例封装了 Java 应用程序的运行时环境。你不能直接创建这个类的实例,而是通过调用 Runtime.getRuntime() 方法来获取当前 Java 应用程序的运行时对象。
3.1 Runtime 类中的 getRuntime() 方法
@Test
void systemRunTimeTest(){
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime); //java.lang.Runtime@7ccfdaef
}
3.2 Runtime 类中的 exit() 方法
可用于停止当前运行的虚拟机
@Test
void systemRunTimeTest(){
Runtime runtime = Runtime.getRuntime();
runtime.exit(0);
System.out.println("执行当前代码");
}