目录
API之Math
用于数学计算的工具类
获取绝对值
//获取绝对值
System.out.println(Math.abs(-1023));//1023
System.out.println(Math.abs(1023));//1023
向上取整
//向上取整
System.out.println(Math.ceil(10.23));//11.0
System.out.println(Math.ceil(10.52));//11.0
System.out.println(Math.ceil(-10.23));//-10.0
System.out.println(Math.ceil(-10.52));//-10.0
向下取整
//向下取整
System.out.println(Math.floor(10.23));//10.0
System.out.println(Math.floor(10.52));//10.0
System.out.println(Math.floor(-10.23));//-11.0
System.out.println(Math.floor(-10.52));//-11.0
四舍五入
//四舍五入
System.out.println(Math.round(10.23));//10
System.out.println(Math.round(10.52));//11
System.out.println(Math.round(-10.23));//-10
System.out.println(Math.round(-10.52));//-11
获取两个整数的较大值
//获取两个整数的较大值
System.out.println(Math.max(10,20)); //20
获取两个整数的较小值
//获取两个整数的较小值
System.out.println(Math.min(10,20));//10
获取平方根
//获取平方根
System.out.println(Math.sqrt(4));//2.0
获取立方根
//获取立方根
System.out.println(Math.cbrt(8));//2.0
获取一个数的几次幂
//获取一个数的几次幂
System.out.println(Math.pow(2, 3));//8.0
获取随机数
//获取随机数
System.out.println(Math.random()); //范围为[0.0,1.0)
API之System
提供与系统相关的方法的工具类
终止当前运行的JAVA虚拟机
//0表示虚拟机正常停止
System.exit(0);
//非0表示虚拟机异常停止
System.exit(1);
返回当前系统的时间毫秒值
//返回当前系统的时间毫秒值
long l = System.currentTimeMillis();
System.out.println(l); //1709106966189
数组拷贝
//拷贝数组
int[] arr1 = {1,2,3,4,5,6,7,8,9};
int[] arr2 = new int[9];
//参数含义 (数据源,数据源拷贝开始索引,拷贝地,拷贝地开始索引,拷贝个数)
System.arraycopy(arr1,0,arr2,0,9);
//输出arr2
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
//1 2 3 4 5 6 7 8 9
API之Runtime
表示当前虚拟机的运行环境
获取Runtime对象
//获取Runtime对象
Runtime r = Runtime.getRuntime();
终止当前运行的JAVA虚拟机
//终止当前运行的JAVA虚拟机 0表示正常停止
Runtime.getRuntime().exit(0);
//非0表示异常停止
Runtime.getRuntime().exit(1);
获取CPU的线程数
//获取CPU的线程数
System.out.println(Runtime.getRuntime().availableProcessors());//可能不一样
虚拟机能从系统中获取总内存大小
//虚拟机能从系统中获取总内存大小 单位字节(byte)
System.out.println(Runtime.getRuntime().maxMemory());
虚拟机已经从系统中获取总内存大小
//虚拟机已经从系统中获取总内存大小 单位字节(byte)
System.out.println(Runtime.getRuntime().totalMemory());
虚拟机剩余内存大小
//虚拟机剩余内存大小 单位字节(byte)
System.out.println(Runtime.getRuntime().freeMemory());
运行cmd命令
//运行cmd命令 例如下面打开记事本
Runtime.getRuntime().exec("notepad"); //打开记事本
API之Object
Object是Java中的超级父类,所有的类都直接或间接继承于Object类
返回对象的字符串表示形式
//返回对象的字符串表示形式
Object obj = new Object();
String str = obj.toString();
System.out.println(str); //打印的是包名加地址 java.lang.Object@7f31245a
比较两个对象是否相等
//比较两个对象是否相等
Object obj1 = new Object();
Object obj2 = new Object();
System.out.println(obj1.equals(obj2)); //false
对象克隆
也叫对象拷贝,对象复制
测试类
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
//创建对象
Student s1 = new Student("张三",20);
//克隆对象
Object s2 = (Student) s1.clone();
System.out.println(s1); //Student{name='张三', age=20}
System.out.println(s2); //Student{name='张三', age=20}
}
}
浅克隆
不管对象内部属性是基本数据类型和引用数据类型,都完全拷贝过来
浅克隆
基本数据类型拷贝,字符串复用串池的,引用数据类型会创建新的
API之Objects
方法 | 说明 |
---|---|
public static boolean equals(Object obj1,Object obj2) | 先做非空判断,在比较两个对象 |
public static boolean isNull(Object obj) | 判断对象是否为空 空返回true 反之返回false |
public static boolean nonNull(Object obj) | 判断对象是否为空,空返回false 反之返回true |