常用API
1. Math类
Math类是用于数学计算的类,不需要创建对象就可以调用类中的方法,如求绝对值、四舍五入、求某个数的n次幂等,以下为一些常见的Math方法示例:
1 2 3 4 5 6 7 8 9 10 11
| public class test1 { public static void main(String[] args) { System.out.println(Math.abs(-1)); System.out.println(Math.ceil(3.21)); System.out.println(Math.floor(4.56)); System.out.println(Math.round(5.51)); System.out.println(Math.max(3,5.4) + Math.min(3,5.4)); System.out.println(Math.pow(3.5,3)); System.out.println(Math.sqrt(4) + Math.cbrt(8)); } }
|
Math类也定义了一些较高精度的数学常数,如圆周率pi和自然常数e等
1 2 3 4 5 6
| public static final double E = 2.718281828459045; public static final double PI = 3.141592653589793; public static final double TAU = 2.0 * PI; private static final double DEGREES_TO_RADIANS = 0.017453292519943295; private static final double RADIANS_TO_DEGREES = 57.29577951308232;
|
2. System类
System类是与系统相关的类,提供了操作系统相关的方法,不需要创建对象就可以调用类中的方法,以下是示例方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.util.Arrays;
public class test1 { public static void main(String[] args) { System.out.println(System.currentTimeMillis());
int[] arr = {1,2,3,4,5,6}; int[] arr1 = new int[3]; System.arraycopy(arr, 3, arr1, 0, 3); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr1));
System.exit(0); } }
|
其中时间原点为1970年1月1日0时0分0秒(UTC+0)
3. Runtime类
Runtime类与程序运行的环境有关,也不需要创建对象就可以调用类中的方法,以下为一些常见的方法示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import java.io.IOException;
public class test1 { public static void main(String[] args) throws IOException { Runtime rt = Runtime.getRuntime(); System.out.println(rt.availableProcessors()); System.out.println(rt.maxMemory()); System.out.println(rt.totalMemory()); System.out.println(rt.freeMemory()); rt.exec(new String[]{"notepad"}); rt.exit(0); } }
|
4. Object和Objects类
Object类是Java中的最高父类,所有的类都直接或间接继承于Object类