1.Math
- 是一个帮助我们用于数学计算的工具类
- 私有化构造方法,所有的方法都是静态的
Math类的常用方法
方法名 | 说明 | |
public static int | abs(int a) | 获取参数绝对值 |
public static double | ceil(double a) | 向上取整 |
public static double | floor(double a) | 向下取整 |
public static int | round(float a) | 四舍五入 |
public static int | max(int a,int b) | 获取两个int中的较大值 |
public static double | pow(double a,double b) | 返回a的b次幂的值 |
public static double | random() | 返回值为double的随机值,范围[0.0 , 1.0) |
public static void main(String[] args) {
//abs 获取参数绝对值
System.out.println(Math.abs(88));//88
System.out.println(Math.abs(-88));//88
//bug:
//以int为例,取值范围-2147483648 ~ 2147483647
System.out.println(Math.abs(-2147483648));//-2147483648
//ceil 向上取整
System.out.println(Math.ceil(12.34));//13.0
System.out.println(Math.ceil(12.54));//13.0
System.out.println(Math.ceil(-12.54));//-12.0
System.out.println(Math.ceil(-12.54));//-12.0
//floor 向下取整
System.out.println(Math.floor(12.44));//12.0
System.out.println(Math.floor(12.55));//12.0
System.out.println(Math.floor(-12.44));//-13.0
System.out.println(Math.floor(-12.55));//-13.0
//round 四舍五入
System.out.println(Math.round(12.45));//12
System.out.println(Math.round(12.54));//13
//max 获取较大值
System.out.println(Math.max(12.3, 13.2));//13.2
System.out.println(Math.max(10, 20));//20
//min 获取两数中较小值
System.out.println(Math.min(20, 30));//20
//pow 获取a的b次幂
System.out.println(Math.pow(2, 3));//8.0
System.out.println(Math.pow(4,0.5));//2.0
System.out.println(Math.pow(2, -1));//0.5
//sqrt 获取a的平方根
System.out.println(Math.sqrt(4));//2.0
//cbrt 获取a的立方根
System.out.println(Math.cbrt(8));//2.0
//random 获取[0.0 , 1.0)之间的随机数
for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
//获取1~100之间的随机数
for (int i = 0; i < 10; i++) {
System.out.println(Math.floor(Math.random() * 100) + 1);
}
}
2.System
System也是一个工具类,提供了一些与系统相关的方法
System类的常用方法
方法名 | 说明 |
public static void exit(int status) | 终止当前运行的Java虚拟机 |
public static long currentTimeMillis() | 返回从时间原点开始到代码运行这个时间点在这个过程中一共过了多少毫秒 |
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数) | 数组拷贝 |
public class SystemDemo2 {
public static void main(String[] args) {
//exit 停止虚拟机运行
//状态码 0:虚拟机正常停止 非0:虚拟机异常停止
//System.exit(0);
//System.out.println("看看我执行了吗");
//应用:当我们需要把整个程序结束的时候,就可以调用这个方法
//currentTimeMillis 获取时间毫秒值
long l = System.currentTimeMillis();
System.out.println(l);
//应用:计算程序执行需要的时间
//arraycopy 拷贝数组
int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
int[] arr2 = new int[10];
System.arraycopy(arr1,0,arr2,0,10);
for (int i = 0; i < arr2.length; i++) {
int number = arr2[i];
System.out.print(number + " ");
}
//arr2: 0 0 0 0 1 2 3 0 0 0
System.arraycopy(arr1,0,arr2,4,3);
//细节:
//1.如果数据源数组和目的地数组都是基本数据类型,那么两者的类型必须保持一致,否则会报错
//2.在拷贝的时候需要考虑数组的长度,如果超出范围也会报错
//3.如果数据源数组和目的地数组都是引用数据类型,那么子类类型可以赋值给父类类型
}
}
3.Runtime
Runtime表示当前虚拟机的运行环境
Runtime类的常用方法
方法名 | 说明 |
public static Runtime getRuntime() | 当前系统的运行环境 |
public void exit(int status) | 停止虚拟机 |
public int availableProcessors() | 获取CPU线程数 |
public long maxMemory() | JVM能从系统中获取总内存大小(单位byte) |
public long totalMemory() | JVM已经从系统中获取总内存大小(单位byte) |
public long freeMemory() | JVM剩余内存大小(单位byte) |
public Process exec(String command) | 运行cmd命令 |
import java.io.IOException;
public class RuntimeDemo1 {
public static void main(String[] args) throws IOException {
//获取Runtime对象
//Runtime对象表示当前系统的运行环境,所有Runtime对象都表示同一个地址值
Runtime r1 = Runtime.getRuntime();
Runtime r2 = Runtime.getRuntime();
System.out.println(r1 == r2);//true
//exit 停止虚拟机
//Runtime.getRuntime().exit(0);
//System.out.println("看看我执行了吗");
//获取CPU线程数
System.out.println(Runtime.getRuntime().availableProcessors());//8
//获取总内存大小,单位byte
System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);//1804
//已经获取的总内存大小
System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);//123
//剩余内存大小
System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);//119
//运行cmd命令
Runtime.getRuntime().exec("notepad");//打开记事本
//shutdown :关机 加上参数才能执行
//-s :默认在一分钟后关机
//-s -t 指定时间 :指定关机时间
//-a :取消关机操作
//-r :关机并重启
Runtime.getRuntime().exec("shutdown -s -t 3600");//1小时后关机
Runtime.getRuntime().exec("shutdown -a");//取消关机操作
}
}