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 double pow(double a,double b)a的b次幂
- public static double sqrt(double a)a的p平方根
- public static double cbrt(double a)a的立方根
- public static int max(int a,int b)获取两个值中更大的那个
- public static double random()获取[0.0~1.0)的随机数
public class Main {
public static void main(String[] args) {
System.out.println(Math.abs(-9));
System.out.println(Math.ceil(3.7));
System.out.println(Math.floor(3.7));
System.out.println(Math.round(4.4));
System.out.println(Math.pow(2, 3));
System.out.println(Math.sqrt(4));
System.out.println(Math.cbrt(8));
System.out.println(Math.max(5, 8));
System.out.println(Math.random());
}
}
运行结果
9
4.0
3.0
4
8.0
2.0
2.0
8
0.9730480752347369
System类
- public static void exit(int status)终止当前运行的Java虚拟机,一般写入0
- public static long currentTimeMillis() 毫秒值形式返回当前距1970.1.1 8:00至今的系统时间
- public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数) 数组拷贝
public class Main {
public static void main(String[] args) {
long start=System.currentTimeMillis();
System.out.println(start);
int[] arr1={1,2,3,4,5,6};
int[] arr2=new int[6];
//从arr1第一个开始拷贝,拷贝到arr2中3第四到六2个元素
System.arraycopy(arr1,0,arr2,3,2);
for (int i=0;i<6;i++)
System.out.print(arr2[i]+" ");
}
}
运行结果
Runtime类
- public static Runtime getRuntime()当前系统的运行环境对象
- public void exit(int status)终止虚拟机
- public int availableProcessors()获得CPU的线程数
- public long maxMemory()JVM能从系统重获取的总内存大小(单位byte)
- public long totalMemory() JMV已经获取的总内存大小(单位byte)
- public long freeMemory() JMV剩余内存大小
- public Process exec(Sting command) 运行cmd命令
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Runtime r1=Runtime.getRuntime();
Runtime r2=Runtime.getRuntime();
System.out.println(r1 == r2);
//线程
System.out.println(Runtime.getRuntime().availableProcessors());
//总内存(除1024*1024得到MB为单位的大小)
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024);
//已获取内存
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024);
//剩余内存
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024);
//运行cmd,打开记事本
System.out.println(Runtime.getRuntime().exec("notepad"));
//终止
Runtime.getRuntime().exit(0);
//不执行
System.out.println(1);
}
}
运行结果
Object类
- public Object() 空参构造
- public String toString() 返回对象字符串形式
- public boolean equals(Object obj) 比较两个对象是否相等
- protected Object clone(int a) 对象克隆(需要自己重写clone方法)
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, CloneNotSupportedException {
Object ob=new Object();
//打印: 包名+类名+@+地址值
System.out.println(ob.toString());
Student s1=new Student();
Student s2=new Student();
//默认比较地址值,如果比较属性值需要重写equals函数
boolean res=s1.equals(s2);
System.out.println(res);
Student s3=new Student();
Student s4=(Student) s3.clone();
}
}
public class Student implements Cloneable{
String name;
int num;
public Student() {
}
public Student(String name, int num) {
this.name = name;
this.num = num;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}