String Handling

Java 12 min min read Updated: Mar 31, 2026 Beginner
String Handling
Beginner Topic 3 of 14

Fundamental Classes – Object, Class, System, Runtime

Java provides many built-in classes, but some classes are so fundamental that they form the base of the entire language and runtime environment. Among these, the most important are Object, Class, System, and Runtime.

These classes are part of the java.lang package, which is automatically imported in every Java program. They are deeply connected with object creation, class metadata, input-output, memory management, process execution, and JVM interaction.

Key Concept: Object is the root class of Java, Class represents metadata about classes, System provides system-level utility features, and Runtime allows interaction with the JVM runtime environment.

Why These Classes Are Called Fundamental

These classes are called fundamental because they support the core behavior of Java itself.

  • Object is the parent of almost every Java class
  • Class provides runtime information about classes and objects
  • System gives access to standard input, output, properties, and utility methods
  • Runtime gives access to memory information, external process execution, and JVM-related operations

1. Object Class

The Object class is the root class of the Java class hierarchy. Every class in Java directly or indirectly inherits from Object.

That means every object in Java automatically gets access to the methods of the Object class.

Simple Understanding

If you create any class:

java class Student { }

It is automatically treated like:

java class Student extends Object { }

Common Methods of Object Class

Some important methods of the Object class are:

  • toString()
  • equals()
  • hashCode()
  • getClass()
  • clone()
  • finalize()
  • wait()
  • notify()
  • notifyAll()

toString() Method

The toString() method returns a string representation of an object.

Example Without Overriding

java class Student { } public class Main { public static void main(String[] args) { Student s = new Student(); System.out.println(s); } }

Output may look like:

text Student@15db9742

This default representation is not very readable.

Example With Overriding

java class Student { String name = "Amit"; public String toString() { return "Student name is " + name; } } public class Main { public static void main(String[] args) { Student s = new Student(); System.out.println(s); } }

equals() Method

The equals() method is used to compare objects logically.

By default, it checks reference equality, but it can be overridden for content comparison.

Example

java String s1 = new String("Java"); String s2 = new String("Java"); System.out.println(s1 == s2); // false System.out.println(s1.equals(s2)); // true

Here:

  • == compares references
  • equals() compares content

hashCode() Method

The hashCode() method returns an integer hash value representing the object. It is heavily used in collections such as HashMap and HashSet.

If two objects are equal according to equals(), then they should return the same hash code.

getClass() Method

The getClass() method returns the runtime class information of an object.

Example

java class Student { } public class Main { public static void main(String[] args) { Student s = new Student(); System.out.println(s.getClass().getName()); } }

This method connects Object class to reflection and runtime type inspection.

wait(), notify(), notifyAll()

These methods are used in multithreading for inter-thread communication.

  • wait() → current thread waits
  • notify() → wakes up one waiting thread
  • notifyAll() → wakes up all waiting threads

These methods are defined in Object because every object can act as a monitor/lock.

2. Class Class

The Class class represents metadata about a Java class at runtime. In simple words, it stores information about classes such as:

  • class name
  • methods
  • fields
  • constructors
  • modifiers
  • annotations

Every loaded class in Java has a corresponding Class object.

How to Get Class Object

There are mainly three common ways:

1. Using class literal

java Class c = Student.class;

2. Using getClass()

java Student s = new Student(); Class c = s.getClass();

3. Using Class.forName()

java Class c = Class.forName("Student");

Example of Class Class Usage

java class Student { } public class Main { public static void main(String[] args) { Student s = new Student(); Class c = s.getClass(); System.out.println(c.getName()); } }

This prints the class name at runtime.

Common Methods of Class Class

  • getName()
  • getSimpleName()
  • getDeclaredMethods()
  • getDeclaredFields()
  • getDeclaredConstructors()
  • getSuperclass()
  • getInterfaces()

Example of getSimpleName()

java System.out.println(Student.class.getSimpleName());

Example of getSuperclass()

java System.out.println(Student.class.getSuperclass().getName());

Since every class extends Object, this usually prints:

text java.lang.Object

Why Class Class is Important

The Class class is the foundation of reflection. It allows Java to inspect types dynamically at runtime.

Many frameworks such as Spring, Hibernate, and JUnit use this internally.

3. System Class

The System class provides access to system-level features such as standard input, standard output, environment information, current time, array copying, and JVM interaction.

It belongs to java.lang and is automatically available.

Common Members of System Class

  • System.out
  • System.in
  • System.err
  • currentTimeMillis()
  • arraycopy()
  • getProperty()
  • gc()
  • exit()

System.out

System.out is the standard output stream used to print output to the console.

java System.out.println("Hello Java");

System.in

System.in is the standard input stream used for keyboard input.

java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.println(name); } }

System.err

System.err is the standard error output stream. It is often used for error messages.

java System.err.println("This is an error message");

currentTimeMillis()

This method returns the current time in milliseconds since January 1, 1970 UTC.

java long time = System.currentTimeMillis(); System.out.println(time);

It is useful for measuring execution time.

Example

java long start = System.currentTimeMillis(); for (int i = 0; i < 1000000; i++) { } long end = System.currentTimeMillis(); System.out.println("Time: " + (end - start) + " ms");

arraycopy()

The arraycopy() method copies elements from one array to another.

java int[] source = {1, 2, 3, 4}; int[] destination = new int[4]; System.arraycopy(source, 0, destination, 0, source.length);

getProperty()

This method is used to get system properties such as Java version, operating system name, or user directory.

java System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("os.name"));

gc()

The gc() method requests garbage collection.

java System.gc();

It does not guarantee immediate garbage collection, but it requests JVM to perform it.

exit()

The exit() method stops the current Java program.

java System.exit(0);

Here, 0 usually means normal termination.

Why System Class is Important

The System class is used almost daily in Java programming because it provides basic I/O, timing, environment information, and runtime interaction.

4. Runtime Class

The Runtime class represents the runtime environment of the Java application. It allows the program to interact with the JVM itself.

Using the Runtime class, we can:

  • get memory information
  • request garbage collection
  • execute external programs
  • check available processors

Getting Runtime Object

The Runtime class cannot be instantiated directly using new. It uses the singleton-style access method:

java Runtime r = Runtime.getRuntime();

Common Methods of Runtime Class

  • totalMemory()
  • freeMemory()
  • availableProcessors()
  • gc()
  • exec()

totalMemory() and freeMemory()

These methods provide information about JVM memory.

java Runtime r = Runtime.getRuntime(); System.out.println("Total Memory: " + r.totalMemory()); System.out.println("Free Memory: " + r.freeMemory());

This is useful for understanding memory behavior of applications.

availableProcessors()

This method returns the number of processors available to the JVM.

java Runtime r = Runtime.getRuntime(); System.out.println("Processors: " + r.availableProcessors());

gc() in Runtime Class

This method is similar to System.gc() and requests garbage collection.

java Runtime.getRuntime().gc();

exec() Method

The exec() method can be used to run external system commands or applications.

Example

java Runtime r = Runtime.getRuntime(); r.exec("notepad");

This may open Notepad on Windows.

On Linux or macOS, the command would differ depending on the installed application.

System Class vs Runtime Class

Point System Class Runtime Class
Purpose General system-level utilities Interaction with JVM runtime environment
Examples out, in, err, currentTimeMillis() memory info, gc(), exec()
Object creation Static utility class style usage Accessed through getRuntime()

Relationship Between Object, Class, System, and Runtime

These classes work together in the Java language and runtime environment:

  • Object gives base behavior to all Java classes
  • Class provides runtime metadata
  • System gives standard utility and environment access
  • Runtime provides deeper JVM runtime interaction

Practical Example Using Multiple Fundamental Classes

java class Student { String name = "Amit"; } public class Main { public static void main(String[] args) { Student s = new Student(); // Object class method System.out.println(s.toString()); // Class class method System.out.println(s.getClass().getName()); // System class method System.out.println(System.currentTimeMillis()); // Runtime class method Runtime r = Runtime.getRuntime(); System.out.println("Free Memory: " + r.freeMemory()); } }

This shows how all these classes are useful in real Java code.

Common Mistakes

  • Confusing Object class with object instance
  • Thinking Class class is same as normal user-defined class
  • Overusing System.gc() expecting guaranteed collection
  • Using exec() carelessly without understanding platform dependency
  • Not understanding difference between System utilities and Runtime JVM interaction

Best Practices

  • Override toString() for meaningful object printing
  • Use equals() and hashCode() carefully in object comparison
  • Use Class information when reflection or dynamic logic is required
  • Use System for standard utility operations
  • Use Runtime carefully for memory or process-related operations

Interview-Oriented Points

  • Object is the parent of almost every Java class
  • toString(), equals(), and hashCode() come from Object
  • Class represents runtime metadata of a class
  • System provides I/O, system properties, and utility methods
  • Runtime provides JVM-related operations such as memory info and process execution
  • System.gc() and Runtime.getRuntime().gc() both request garbage collection
  • getClass() returns the runtime type of an object

Conclusion

The classes Object, Class, System, and Runtime are among the most essential built-in classes in Java. They support object behavior, runtime metadata, system-level utilities, and JVM interaction.

A strong understanding of these classes gives deeper insight into how Java works internally and how Java programs interact with both the language and the runtime environment.

Quick Summary: Object is the base class of Java, Class provides runtime class information, System offers core utilities, and Runtime lets Java programs interact with the JVM environment.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators