system java api_Java8 API学习13 - java.lang.System

System类介绍

The System class contains several useful class fields and methods. It cannot be instantiated.

Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

简单地说, 这是一个和系统环境进行交互的类.

System不允许被实例化, 而且是一个final类

System.in, System.out, System.err

public final static InputStream err = null;

public final static PrintStream out = null;

public final static PrintStream err = null;

由此我们得知, 最常用的这些输入输出类, 并非是一个System的内部类, 而是成员变量.

但是这样问题就来了, 而且是很古怪的问题:

既然其初始值为null, 那我们调用System.out.println()时为什么能正常运行, 其初始化是在哪一步完成的?

既然System.out已经是final类型的null变量, 如何对其重新赋值?

要解决这些问题, 可能需要涉及到Java虚拟机在启动时所作的一些工作. 很遗憾这一部分我了解的很少也不想在此时去深究. 所以对于这一类的特殊情况我想有必要单独放一个节标题来声明:

本文会尽量避免提及System类的实现原理

反过来说, System定义的方法有哪些, 其作用是什么, 这个问题是本文的重点.

回到in, out, err

However, 对于以上两个问题, 还是有必要在尽量不涉及底层的情况下回答一下.

对于问题一, 我参考了https://www.zhihu.com/question/30797907/answer/49481252

和Object类相似, System类中有一个静态代码块

private static native void registerNatives();

static {

registerNatives();

}

根据注释"VM will invoke the initializeSystemClass method to complete the initialization for this class separated from clinit.", initializeSystemClass方法会被调用, 在这一方法中, 完成了对上述成员变量的赋值

对于问题二, 网上到是有不少的回答, 其实这个答案反而简单一点但是我是根本想不到的: native方法不在虚拟机中运行因此可以跳过java中的很多限制, 包括final的修饰.

由于in, out, err都是其他类, 本文不会对它们的方法再进行说明, 请不要期待在这篇文章中找到System.out.println的用法了.

System中定义的方法

native方法

/*把src数组复制到dest数组中, 用Object作参数类型是为了能够以基本类型数组(int[], char[]等)

作为参数, 如果传入的不是一个数组会抛出异常*/

void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

long currentTimeMillis() //获取当前时间戳(单位为毫秒)

//等价于Object.hashCode(), 用于应对hashCode方法被覆盖的情况

int identityHashCode(Object x)

//Maps a library name into a platform-specific string representing a native library.(?)

String mapLibraryName(String)

long nanoTime() //获得当前时间戳(单位为纳秒)

常规方法(1)

void exit(int status) //立刻退出程序, 返回值为status(正常情况下为0)

void gc() //通知JVM运行垃圾回收; 但是不能保证JVM一定执行, 这部分知识请参考关于垃圾回收的文章

void runFinalization() //finalize和gc的区别请参考其他人的文章

Map getenv() //获取操作系统相关信息(environment)

String getenv(String name) //上述方法传入key, 返回value

String lineSeparator() //返回换行符, 即'\n'或'\r\n'

//重定向System.in, System.out和System.err

void setOut(PrintStream)

void setErr(PrintStream)

void setIn(InputStream)

常规方法(2), 不明白的方法

//Removes the system property indicated by the specified key.

String clearProperty(String)

//Returns the unique Console object

Console console()

Properties getProperties()

String getProperty(String) //上述方法获取单个property

String setProperty(String key, String value) //重新设置property, 返回原value

void setProperties(Properties)

SecurityManager getSecurityManager()

void setSecurityManager(SecurityManager)

Channel inheritedChannel()

//Loads the native library specified by the filename argument.

void load(String)

//Loads the native library specified by the libname argument.

void loadLibrary(String)

总结

除了输入输出以外, 可能用到的也只有System.getCurrentMillis()了, 另外arraycopy可能在某些时候很有用; 不过这里有一个我很在意的Properties类, 在做框架(比如Spring)配置的时候见到过但是不明白其具体含义和用法, 不知道能否有写到这个类的那一天呢...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值