HOME HTML EDITOR C JAVA PHP

Java System Methods

The System class in Java is part of the java.lang package.

It provides useful methods and fields for input, output, memory management, time, and system-related operations.

The System class is final and all its members are static. So, we do not need to create an object of it.

1. System.out.println()

The println() method prints output to the console and moves the cursor to the next line.

System.out.println("Hello World");

2. System.out.print()

The print() method prints output but does not move to the next line.

System.out.print("Hello "); System.out.print("World");

3. System.err.println()

The err is used to print error messages.

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

4. System.in

The in is used to take input from the user (usually with Scanner class).

import java.util.Scanner; Scanner sc = new Scanner(System.in); int number = sc.nextInt(); System.out.println(number);

5. currentTimeMillis()

The currentTimeMillis() method returns the current time in milliseconds.

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

6. nanoTime()

The nanoTime() method returns the current time in nanoseconds. It is used to measure execution time.

long start = System.nanoTime(); // code here long end = System.nanoTime(); System.out.println(end - start);

7. exit()

The exit() method stops the program execution.

System.exit(0);

0 means normal termination.

8. gc()

The gc() method requests the Java Virtual Machine to run Garbage Collection.

System.gc();

9. getenv()

The getenv() method returns system environment variables.

String path = System.getenv("PATH"); System.out.println(path);

10. getProperty()

The getProperty() method returns system properties.

String os = System.getProperty("os.name"); System.out.println(os);

Common System Properties

Why System Class is Important?

Important Points

Conclusion

The System class is one of the most important classes in Java.

It provides essential methods like print(), println(), exit(), currentTimeMillis(), and more.

Every Java program uses the System class in some way.