Java - 为什么系统类和运行时类具有相同的方法?
我最近想知道为什么 java.lang.Runtime
和 java.lang.System
具有相同的库加载、垃圾收集和类似操作的方法。是因为历史原因、为了方便,还是它们确实不同?这两个类都可以从 JDK 1.0 中获得...
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. Is it because of historical reasons, for convenience, or they really differ? Both classes are available from JDK 1.0...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测(记住,这是一个猜测),System 类中的方法是为了方便起见。例如,
System.gc();
是静态方法,其中Runtime.gc();
是实例方法。这使得调用更容易,因为您不需要获取Runtime
实例。My guess (remember, its a guess), is that methods in the
System
class are there for convenience. For example,System.gc();
is static, whereRuntime.gc();
is an instance method. This makes the call easier to make, since you don't need to obtain aRuntime
instance.系统公开了开发人员可能适合使用该系统的各种事物。
我会担心程序员直接使用运行时。为了让系统调用这些方法,需要公开它们。
系统提供了一个运行时接口,以允许访问适合程序员调用的运行时方法。调用系统方法并让它们适当地委托。
System exposes various things it might be appropriate for a developer to use the System for.
I would be concerned about a programmer playing directly with the Runtime. For System to call the methods, they need to be exposed.
System provides an interface to the Runtime to enable access to Runtime methods that are appropriate to be called by programmers. Call the System methods and let them delegate appropriately.
例如,如果查看方法
System#load(String)
,您会发现它调用方法Runtime#load(String)
。与gc()
相同。所以这很可能是出于历史原因。If you look for example at the method
System#load(String)
, you see, that it calls the methodRuntime#load(String)
. Same forgc()
. So it is most probably for historical reasons.