简体   繁体   English

Java - 为什么System和Runtime类具有相同的方法?

[英]Java - why System and Runtime classes have identical methods?

I was wondering recently why do both java.lang.Runtime and java.lang.System have the same methods for library loading, garbage collecting and similar operations. 我最近想知道为什么java.lang.Runtimejava.lang.System都有相同的库加载,垃圾收集和类似操作的方法。 Is it because of historical reasons, for convenience, or they really differ? 是因为历史原因,为了方便,还是真的不同? Both classes are available from JDK 1.0... 这两个类都可以从JDK 1.0获得......

My guess (remember, its a guess), is that methods in the System class are there for convenience. 我的猜测(请记住,这是一个猜测), System类中的方法是为了方便起见。 For example, System.gc(); 例如, System.gc(); is static, where Runtime.gc(); 是静态的,其中Runtime.gc(); is an instance method. 是一个实例方法。 This makes the call easier to make, since you don't need to obtain a Runtime instance. 这使得调用更容易,因为您不需要获取Runtime实例。

System exposes various things it might be appropriate for a developer to use the System for. 系统公开了开发人员使用System可能适合的各种事情。

I would be concerned about a programmer playing directly with the Runtime. 我会担心程序员直接玩Runtime。 For System to call the methods, they need to be exposed. 要使System调用方法,需要公开它们。

System provides an interface to the Runtime to enable access to Runtime methods that are appropriate to be called by programmers. System为Runtime提供了一个接口,以便能够访问适合程序员调用的Runtime方法。 Call the System methods and let them delegate appropriately. 调用System方法并让它们适当地委派。

If you look for example at the method System#load(String) , you see, that it calls the method Runtime#load(String) . 如果您查看System#load(String)方法的示例,您会看到它调用方法Runtime#load(String) Same for gc() . 对于gc() So it is most probably for historical reasons. 所以这很可能是出于历史原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM