Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 436
zeller Avatar asked Nov 17 '11 13:11

zeller


2 Answers

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.

like image 60
Vivien Barousse Avatar answered Oct 15 '22 05:10

Vivien Barousse


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.

like image 38
BillThor Avatar answered Oct 15 '22 06:10

BillThor