Difference Between "RUNTIME" Class And "CLASS" Class

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

  • long freeMemory(): Determines the amount of free memory available to the java runtime system in terms of bytes.
  • long totalMemory(): Returns the total amount of memory in the Java Virtual machine. It is measured in bytes.
  • long maxMemory(): Returns the maximum amount of memory Java Virtual Machine will use. If there is no limit, then it returns Long.MAX.VALUE
  • Runtime getRuntime(): Returns the current Runtime instance.
  • Process exec(String name)throws IOException: Executes a separate program whose name is given.

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:

  • Use getclass() method in an object.
  • Use forName() method to get an instance of class using the name of the class.
  • Load a new class using a custom ClassLoader object.

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

  • Boolean equals(Object obj): Compares current object instance with given object and checks if they are equal.
  • void finalize(): Default form of the finalize method. Usually overridden by subclasses.
  • String toString():  Returns a string representation of the object.
  • void wait(): Causes the current thread to enter a waiting state.

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


Similar Articles