Introduction

The Runtime class encapsulates the runtime environment. In a running Java application, the instances of this class encapsulates the run time state of an object.

Runtime class

Basically, the runtime class is used for memory management and executing additional processes. Every Java Program has a single instance of this class to allow the application to act as an interface with the environment. The current runtime instance can be obtained from the getRuntime() method.

Garbage collection in java is an automatic process and runs periodically. The garbage collector can be manually invoked by calling gc() method on the current runtime instance. Also, the programmer can determine memory allocation details by using totalMemory() and freeMemory() method.

For example,

class abc
{
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    Runtime rt= Runtime.getRuntime();
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    long memofree=rt.freeMemory();
    long memotot=rt.totalMemory();
    rt.gc();
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
}

Methods used of Runtime class

Source Code

class DemosRuntime {
    protected DemosRuntime() {}
    public static void main(final String[] args) {
        Runtime runObj = Runtime.getRuntime();
        Process processObj = null;
        try {
            processObj = runObj.exec("calc.exe");
        } catch (Exception e) {
            System.out.println("Error Execution occur to perform calculator");
        }
    }
}

Output

Difference between “RUNTIME” Class and “CLASS” class

The current Runtime instance is obtained by means of the Runtime.getRuntime() method. A reference to the Executable program calc.exe is obtained and stored in an object of type Process.

"Class" class

In a running Java application, the instances of this class Encapsulate the run time state of an object. Object of this class is created automatically and need not be declared. This allows to retrieve information about the object during runtime.
An instance or an object of this class can be obtained in one of three ways:

The getSuperClass() method returns the superclass of the object invoked. There is no public constructor for class.

Source Code

class StringStore {
    protected StringStore() {}
}
class IntegerStore extends StringStore {
    //protected StringStore() {}
}
class DemoClass {
    protected DemoClass() {}
    public static void main(final String[] args) {
        StringStore objStngstr = new StringStore();
        IntegerStore objIntstr = new IntegerStore();
        Class objClass;
        objClass = objStngstr.getClass();
        System.out.println("ObjString is Object of Type :" + objClass.getName());
        objClass = objIntstr.getClass();
        System.out.println("Integer Object is object of Type :" + objClass.getName());
        objClass = objClass.getSuperclass();
        System.out.println("Integer Object superclass is :" + objClass.getName());
    }
}

Output

The object class is the superclass of all classes. Even if a user-defined class does not extend from any other class, it extends from the object class by default.

Methods used of the Class class

Source Code

class DemonsObject {
    protected DemonsObject() {}
    public static void main(String args[]) {
        if (args.length > 0)
            if (args[0].equals("Java")) {
                System.out.println("Java is the Programming Language");
            }
        else System.out.println("DemonsObject Java");
    }
}

Output

Difference between “RUNTIME” Class and “CLASS” class

In this program, no class or package has been imported. The programmer can make use of the equals() method of an object class. This is because any user-defined class (such as DemonsObject) in Java is by default inherited from the object class. The object class is part of java.lang package. This package does not require an explicit import statement.

Summary

The Runtime class encapsulates the runtime environment and is typically used for memory management and running additional program