系统相关类:

来源:互联网 发布:网络打鸡血是什么意思 编辑:程序博客网 时间:2024/06/06 10:51

系统相关类:
java程序在不同操作系统上运行时,可能需要获得平台相关的属性,或者调用平台命令来完成特定的功能。java提供了System类和Runtime类来与程序运行的平台进行交互。

System类:System提供了代表标准输入、标准输出和错误输出的类变量,并提供了一些静态方法用于访问环境变量、系统属性的方法,还提供了加载文和动态链接库的方法。
(1)System通过调用System类的getenv() getProperties() getProperty() 等方法来访问程序所在平台的环境变量和系统属性。
(2)System类还有两个获取系统当前时间的方法:currentTimeMillis()和nanlTime(),他们都返回一个Long型整数。
(3)System类还提供了一个identityHashCode(Object x)方法,该方法返回指定对象的hashCode值,也就是根据该对象的地址计算得到的hashCode值。

{    public static void main(String[] args)    {        //s1和s2是不同的两个对象        String s1 = new String("hello");        String s2 = new String("hello");//String重写了hashCode()方法--改为根据字符串序列计算hashCode()值//因为s1和s2字符序列相同,所以他们的hashCode()返回序列相同        System.out.println(s1.hashCode()+"------"+s2.hashCode());        //s1和s2是不同的字符串对象,所以他们的identityHashCode不同            System.out.println(System.identityHashCode(s1)+"------"+System.identityHashCode(s2));            String  s3 = "java";            String s4 ="java";            //s3和s4是相同的字符串对象,所以他们的identityHashCode相同    System.out.println(System.identityHashCode(s3)+"------"+System.identityHashCode(s4));    }}

Runtime类代表java程序的运行环境,每个java程序都有一个与之对应的Runtime实例,应用程序通过该对象与其运行时环境相连。

(1)Runtime类也提供了gc()方法和runFinalization()方法来通知系统进行垃圾回收、清理系统资源,并提供了load(String filename)和loadLibrary(String libname)方法来加载文件和链接数据库.
(2)Runitme类还可以单独启动一个进程来运行操作系统的命令。

public class ExecTset{    public static void main(String[] args)    throws Exception    {        Runtime  rt  =  Runtime.getRuntime();        rt.exec("notepad.exe");  //运行记事本程序        System.out.println("Hello World!");    }}
0 0
原创粉丝点击