1. Math
(1) 代表数学,是一个工具类,里面提供的都是对数据操作的一些静态方法。
(2) 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) {
//public static int abs(int a) 获取参数的绝对值(其他基本类型方法相同)
System.out.println(Math.abs(-123));//123
System.out.println(Math.abs(123));//123
System.out.println(Math.abs(123.03));//123.03
System.out.println(Math.abs(-123.03));//123.03
//public static double ceil(double a) 向上取整
System.out.println(Math.ceil(123.03));//124.0
System.out.println(Math.ceil(123.00));//123.0
//public static double floor(double a) 向下取整
System.out.println(Math.floor(123.06));//123.0
System.out.println(Math.floor(123.00));//123.0
//public static int round(float a) 四舍五入
System.out.println(Math.round(123.606));//124
System.out.println(Math.round(123.404));//123
//public static int max(int a, int b) 获取两个int值中的较大值(其他基本类型方法相同)
System.out.println(Math.max(123,121));//123
System.out.println(Math.max(123.3,121.6));//123.3
//public static double pow(double a, double b) 返回a的b次幂的值
System.out.println(Math.pow(2,3));//8.0
System.out.println(Math.pow(2.6,3.6));//31.182006138003036
//public static double random() 返回double 类型的随机值范围[0.0,1.0]
System.out.println(Math.random());//0.6261460302959388 随机数
}
2. System
(1) System代表程序所在的系统,也是一个工具类
(2) System类常见方法
方法 | 说明 |
public static void exit(int status) | 终止当前运行的Java虚拟机--一般不会使用 |
public static long currentTimeMillis() | 返回当前系统时间的毫秒值(指的是从1970年1月1日 00:00:00到此刻的毫秒数) --1970年1月1日C语言的生日 |
public static void main(String[] args) {
//System
//public static void exit(int status) 终止当前运行的Java虚拟机
//参数用作状态代码,按照惯例 非零状态代码表示异常终止 一般不会使用
//System.exit(0);//
//public static long currentTimeMillis() 返回当前系统时间的毫秒值 一般用作计算程序运行时间
//从1970-01-01 0:0:0开始 到此刻的毫秒值 1s = 1000毫秒
Long time = System.currentTimeMillis();
System.out.println(time);//1734449352827
for (int i = 0; i < 1000000; i++) {
System.out.println(i);
}
System.out.println("for循环共耗时:" + (System.currentTimeMillis() - time));
}
3. Runtime
(1) Runtime代表程序所在的运行环境;是一个单例类
(2) Runtime 常见方法
方法 | 说明 |
public static Runtime getRuntime() | 返回与当前Java应用程序关联的运行时对象 |
public void exit(int status) | 终止当前运行的虚拟机;人为终止虚拟机,不要使用 |
public int availableProcessors() | 返回Java虚拟机可用的处理器数 |
public long totalMemory() | 返回Java虚拟机中的内存数量 |
public long freeMemory() | 返回Java虚拟机中的可用内存 |
public Process exec(String command) throws IOException | 启动某个程序,并返回代表该程序的对象 |
public static void main(String[] args) throws IOException, InterruptedException {
//public static Runtime getRuntime() 返回与当前Java应用程序关联的运行时对象
Runtime runtime = Runtime.getRuntime();
//public void exit(int status) 终止当前运行的虚拟机
//runtime.exit(0);//人为终止虚拟机 不要使用
//public int availableProcessors() 返回Java虚拟机可用的处理器数
System.out.println(runtime.availableProcessors());//
//public long totalMemory() 返回Java虚拟机中的内存数量 返回的字节
System.out.println(runtime.totalMemory() / 1024.0 / 1024.0 + "MB");
//public long freeMemory() 返回Java虚拟机中的可用内存 返回的字节
System.out.println(runtime.freeMemory() / 1024.0 / 1024.0 + "MB");
//public Process exec(String command) throws IOException 参数为启动程序的路径 启动某个程序,并返回代表该程序的对象
Process p =runtime.exec("E:\\QQ\\QQ.exe");
Thread.sleep(5000);//等5000毫秒 = 5s
p.destroy();//销毁/关闭启动某个程序
}