Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

java.lang.System vs java.lang.Runtime

 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 22392
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the Javadoc of java.lang.System and java.lang.Runtime and all will be revealed.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic