简体   繁体   中英

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. Is it because of historical reasons, for convenience, or they really differ? Both classes are available from JDK 1.0...

My guess (remember, its a guess), is that methods in the System class are there for convenience. For example, System.gc(); is static, where Runtime.gc(); is an instance method. This makes the call easier to make, since you don't need to obtain a Runtime 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.

If you look for example at the method System#load(String) , you see, that it calls the method Runtime#load(String) . Same for gc() . So it is most probably for historical reasons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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