Thursday, 4 December 2008

Basics of java.lang

rt.jar contains a lot of the core Java classes, such as those in java.lang, and java.net. When learning the core classes think of what each one is and what they are used for. Try and think of at least three examples in each case (use Google code search to help you).

Examples:

java.lang.Character (implements Serializable, Comparable). Character is a wrapper for a char. Useful e.g. when processing a character input from stdin or from gnu.getopt. Characters can be converted to strings using toString().

java.lang.System (extends Object). This fundamental class contains sytem-wide operations that act on the currently running virtual machine.

  • Exit the VM. exit(statusCode) terminates the VM. A value other than zero indicates abnormal termination
  • Bring in code from a shared library. static void load(String filename), static void loadLibrary(String libname)
  • Garbage collect. System.gc() prompts the VM to run the garbage collector.
  • Reference std input, stderr and stdout. This is done via the static PrintStream fields System.in, System.err and System.out
  • Restrict sensitive operations. This can be done by creating a SecurityManager and calling setSecurityManager on the System class.

java.lang.System is the moral equivalent of java.lang.Runtime; both classes facilitate interaction with the currently running VM. Many methods on System e.g. System.gc, can also be called on the Runtime class, e.g. Runtime.gc.

No comments:

Blog Archive