java基础 21 System类和Runtime类

一、System系统类

1.1、System系统类

    主要用于获取系统信息

1.2、System类的常用方法

arraycopy(Object src, int srcPos, Object dest, int destPos, int length) :从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
     src:源数组
     srcPos:源数组的起始位置
     dest:目标数组
     destPos:目标数组的起始位置
     length:要复制的数组元素的个数
currentTimeMillis() :返回以毫秒为单位的当前时间
exit(int status) :退出虚拟机
gc():运行垃圾回收器
finalize():如果一个对象被垃圾回收器回收的时候,会先调用对象finalize()方法
getenv(String name) :获取指定的环境变量值
getProperties() :获取系统所有的属性
getProperty(String key) :获取指定键指示的系统属性

1.3、实例

 1 package com.zn.system;
 2 
 3 public class Person {
 4     String name;
 5     public Person(String name) {
 6         this.name=name;
 7     }
 8     @Override
 9     protected void finalize() throws Throwable {
10         super.finalize();
11         System.out.println(this.name+"被回收了");
12     }
13 }
 1 package com.zn.system;
 2 
 3 import java.util.Properties;
 4 
 5 public class Demo1 {
 6     public static void main(String[] args) {
 7         int[] srcArr={10,51,22,31,56};
 8         //把srcArr这个数组的元素拷贝到destArr数组
 9         int[] destArr=new int[4];
10         System.arraycopy(srcArr, 1, destArr, 0, 4);
11         for(int i=0;i<destArr.length;i++){
12             System.out.print(destArr[i]);//运行结果:51 22 31 56
13         }
14         
15         long time=System.currentTimeMillis();//从1970年到现在,过了1473071423733毫秒
16         System.out.println(time);//运行结果:1473071423733
17         
18         //System.exit(1); //jvm退出..0或者非0的数据都可以退出jvm.对应用户而言没有任何区别
19         
20         System.out.println(System.getenv("path"));//获取环境变量“path”下的配置值
21         
22         for(int i=0;i<4;i++){
23             new Person("狗娃"+i);
24             System.gc();
25         }
26         
27         Properties p=System.getProperties();//获取系统所有的属性
28         System.out.println(p);
29         p.list(System.out);
30         
31         String value=System.getProperty("os.name");
32         System.out.println("当前系统:"+value);//返回结果:当前系统:Windows 7
33     }
34 }

二、Runtime类

2.1、Runtime类

    该类主要代表应用程序 运行环境

2.2、Runtime类的常用方法

freeMemory():返回java虚拟机中空闲内存
maxMemory():虚拟机试图使用的最多空闲内存
totalMemory() :返回 Java 虚拟机中的内存总量

2.3、实例

1 public class Demo2 {
2     public static void main(String[] args) {
3         Runtime runtime=Runtime.getRuntime();
4         System.out.println(runtime);
5         System.out.println("虚拟机空闲内存:"+runtime.freeMemory());
6         System.out.println("虚拟机试图使用的最多空闲内存:"+runtime.maxMemory());
7         System.out.println("虚拟机的内存总量:"+runtime.totalMemory());
8     }
9 }

原创作者:DSHORE

出自:http://www.cnblogs.com/dshore123/

欢迎转载,转载务必说明出处。(如果本文对你有用,可以点击一下右下角的 推荐,谢谢!

猜你喜欢

转载自www.cnblogs.com/dshore123/p/8978680.html