File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.System vs java.lang.Runtime

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..

1.Whats the difference between System.gc() and Runtime.gc().
2.Whats the difference between System.exit(0) and Runtime.halt(1).

Please put some light on it.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the Javadoc of java.lang.System and java.lang.Runtime and all will be revealed.
 
Jose Monzon
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that's an easy one. As our friend says, the documentation says it all.

Basically System.exit() calls to Runtime.getRuntime().exit(). (yes, because Runtime.exit() is a compile exception. that method is NOT static. The only static method there getInstance() and another one I don't even going to bother to remember because it's deprecated)

Question is, why are there two ways to perform the same functionality? Well, by convention, historical reasons and convenience.

A closer look to the Runtime class tells us that it follows the 'Factory method' pattern. This is, its constructor is private (to prevent you from creating new instances of Runtime) and there is an static reference that you access through Runtime.getRuntime() -the pattern says the method should be called: getInstance() , but anyways... -. The 'Factory method' pattern is a great solution for singleton objects, like in this case. I know you didn't asked this, but nevertheless ;-)

Here, I found an implementation of Runtime: http://www.docjar.com/html/api/java/lang/Runtime.java.html

Funny to see that Runtime.exit() calls to yet another static method Shutdown.exit(). But that's a package-private class you don't even have to worry about.




 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic