![](https://img-blog.csdnimg.cn/a91dfe07b0f246678855997036616561.jpeg#pic_center)
一、Runtime类
💡 每一个JVM进程里面都会存在一个Runtime类对象,它的主要功能是获取运行时的环境属性,或者创建新进程.
Runtime类的常用方法:
![](https://img-blog.csdnimg.cn/img_convert/368c4c05700cff8906c9bca618db226a.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=366&id=wWdxE&margin=[object%20Object]&name=image.png&originHeight=439&originWidth=962&originalType=binary&ratio=1&rotation=0&showTitle=false&size=176348&status=done&style=none&taskId=ua2ba7e13-5ea9-4acf-a5b1-862b47220dc&title=&width=801.6666348112967)
代码:
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("所有可用内存空间:"+runtime.maxMemory());
System.out.println("最大可用内存空间:"+runtime.totalMemory());
System.out.println("空余内存空间:"+runtime.freeMemory());
}
结果:
![](https://img-blog.csdnimg.cn/img_convert/53deaa270d1358c1241a2057b51274e4.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=231&id=qUo7D&margin=[object%20Object]&name=image.png&originHeight=231&originWidth=565&originalType=binary&ratio=1&rotation=0&showTitle=false&size=49046&status=done&style=none&taskId=u295421a5-211a-4bed-a43a-e2721153902&title=&width=565)
- GC(Garbage Collection) 垃圾收集器
我们如果打开一个文件,忘记关闭,时间久了就会导致系统文件描述符表满,从而发生异常(记得close()).为了最大可能的回收垃圾,GC会由JVM不定期的进行自动回收.
代码:
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("所有可用内存空间:"+runtime.maxMemory());
System.out.println("最大可用内存空间:"+runtime.totalMemory());
System.out.println("空余内存空间:"+runtime.freeMemory());
System.out.println("==========================");
runtime.gc();
System.out.println("所有可用内存空间:"+runtime.maxMemory());
System.out.println("最大可用内存空间:"+runtime.totalMemory());
System.out.println("空余内存空间:"+runtime.freeMemory());
}
结果:
![](https://img-blog.csdnimg.cn/img_convert/602c1b87ade36a02552324d6de6180c4.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=334&id=hJCqM&margin=[object%20Object]&name=image.png&originHeight=334&originWidth=707&originalType=binary&ratio=1&rotation=0&showTitle=false&size=85148&status=done&style=none&taskId=u49ee9ed4-dce1-430f-9f50-de98b8f541f&title=&width=707)
2. Random类,这两个类名字相似,干的活却不一样,Random类一般用于 随机数字的生成.
代码:
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 100; i++) {
if(i % 10 == 0) {
System.out.println();
}
System.out.print(random.nextInt(100)+1+" ");
}
}
结果:
![](https://img-blog.csdnimg.cn/img_convert/f15196cc417ca7d439eb0b49f5bfd243.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=307&id=zJsfW&margin=[object%20Object]&name=image.png&originHeight=307&originWidth=652&originalType=binary&ratio=1&rotation=0&showTitle=false&size=70677&status=done&style=none&taskId=u1dbd4361-c1e5-4588-a1a9-8f1cbc02f35&title=&width=652)
二、System类
💡 System类位于java.lang包中,它的构造方法是由private修饰的,所以无法实例化它的对象.
![](https://img-blog.csdnimg.cn/img_convert/c2851305bdd524da138920a76b7584ae.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=124&id=uf1c3b31f&margin=[object%20Object]&name=image.png&originHeight=149&originWidth=507&originalType=binary&ratio=1&rotation=0&showTitle=false&size=20795&status=done&style=none&taskId=u4ef018c4-80c6-4ddf-ad19-64c2d72b36b&title=&width=422.49998321135905)
![](https://img-blog.csdnimg.cn/img_convert/a67017d90e2c46aede5fde1ea0327020.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=377&id=u8475af92&margin=[object%20Object]&name=image.png&originHeight=453&originWidth=1030&originalType=binary&ratio=1&rotation=0&showTitle=false&size=206792&status=done&style=none&taskId=u78c728ea-b1ee-4dd7-aa1e-1cad52c9f87&title=&width=858.3332992262324)
- currentTimeMillis()
功能:
返回以毫秒为单位的当前时间(注意返回类型long).
代码:
public class Test1 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
long a = 0;
for (long i = 0; i < 20_0000_0000; i++) {
a++;
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
运行结果:
![](https://img-blog.csdnimg.cn/img_convert/cb1d4ba47995784808a175294bb0f612.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=171&id=uc5ddbc9d&margin=[object%20Object]&name=image.png&originHeight=205&originWidth=588&originalType=binary&ratio=1&rotation=0&showTitle=false&size=32894&status=done&style=none&taskId=u7df04856-9ea5-44de-9813-4c332e1c7f8&title=&width=489.99998052915015)
功能:
拷贝数组,将原数组复制到目标数组中去.(注意:数组下标越界).
代码:
public class Test1 {
public static void main(String[] args) {
int[] arr1 = {12,23,34,45,56};
int[] arr2 = new int[5];
System.arraycopy(arr1,0,arr2,0,5);
System.out.println(Arrays.toString(arr2));
}
}
运行结果:
![](https://img-blog.csdnimg.cn/img_convert/fdadc6f62c02e8d297cceb59a0e2f01c.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=147&id=u2b656b4d&margin=[object%20Object]&name=image.png&originHeight=177&originWidth=454&originalType=binary&ratio=1&rotation=0&showTitle=false&size=22951&status=done&style=none&taskId=u1b07de4c-596d-4262-84e9-693fa238b5a&title=&width=378.333318299718)
三、Math类
💡 Math类提供了大量的数学方法,以便我们日常的使用.
Math类的常用方法:
![](https://img-blog.csdnimg.cn/img_convert/5006effbb01ca3e38508a64e2a2923fc.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=424&id=u3cc013b3&margin=[object%20Object]&name=image.png&originHeight=424&originWidth=936&originalType=binary&ratio=1&rotation=0&showTitle=false&size=163218&status=done&style=none&taskId=u73d895a1-a827-465b-8502-9ff6b66c1f5&title=&width=936)
Math类的使用相对来说是比较容易上手的,代码如下:
public static void main(String[] args) {
int a = -10;
int b = 9;
System.out.println("最大值: "+Math.max(a, b));
System.out.println("最小值: "+Math.min(a, b));
System.out.println("绝对值: "+Math.abs(a));
System.out.println("开平方"+Math.sqrt(b));
}
结果:
![](https://img-blog.csdnimg.cn/img_convert/a32027b4a01f467b54c465bec50da990.png#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=237&id=ud289e3b7&margin=[object%20Object]&name=image.png&originHeight=237&originWidth=541&originalType=binary&ratio=1&rotation=0&showTitle=false&size=40245&status=done&style=none&taskId=ue18524fa-3b6c-4c00-9bdf-6f8a8423808&title=&width=541)
如上所有的方法,我们知道常用的即可,其他的方法需要是查看文档即可:
https://www.matools.com/api/java8
![](https://img-blog.csdnimg.cn/img_convert/f22a0349b934b96bdaf9df56b3643f58.jpeg#clientId=u8695648a-4357-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u30c9564f&margin=[object%20Object]&name=QQ图片20220905105224.jpg&originHeight=1280&originWidth=1280&originalType=binary&ratio=1&rotation=0&showTitle=false&size=142291&status=done&style=none&taskId=ucfe4463b-e557-4a9e-9365-27ce38c313b&title=)