Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the System
class is part of the java.lang
package and contains several useful class fields and methods. It cannot be instantiated, and all its members are static
, meaning you can access them directly using the class name. Some of the most commonly used features of the System
class include:
The System
class provides three predefined streams:
System.in
: Standard input stream, typically connected to the keyboard.System.out
: Standard output stream, typically connected to the console.System.err
: Standard error stream, typically connected to the console.Example:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name + "!"); System.err.println("This is an error message."); } }
The System
class provides methods to access and modify system properties, such as the operating system name, file separator, and line separator.
System.getProperty(String key)
: Returns the value of the specified system property.System.setProperty(String key, String value)
: Sets the value of the specified system property.Example:
public class Main { public static void main(String[] args) { String osName = System.getProperty("os.name"); System.out.println("Operating System: " + osName); String fileSeparator = System.getProperty("file.separator"); System.out.println("File Separator: " + fileSeparator); String userHome = System.getProperty("user.home"); System.out.println("User Home Directory: " + userHome); } }
The System
class provides methods to get the current time:
System.currentTimeMillis()
: Returns the current time in milliseconds since the epoch (January 1, 1970, 00:00:00 UTC).System.nanoTime()
: Returns the current value of the most precise available system timer, in nanoseconds.Example:
public class Main { public static void main(String[] args) { long currentTimeMillis = System.currentTimeMillis(); System.out.println("Current time in milliseconds: " + currentTimeMillis); long nanoTime = System.nanoTime(); System.out.println("Current time in nanoseconds: " + nanoTime); } }
The System
class provides the arraycopy
method to efficiently copy elements from one array to another.
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
: Copies elements from the src
array to the dest
array.Example:
public class Main { public static void main(String[] args) { int[] srcArray = {1, 2, 3, 4, 5}; int[] destArray = new int[5]; System.arraycopy(srcArray, 1, destArray, 2, 3); for (int i : destArray) { System.out.print(i + " "); } } }
Output:
0 0 2 3 4
Using System.out.println() in Java:
System.out.println()
is used to print data to the standard output stream. It adds a newline after the printed content.
System.out.println("Hello, Java!");
Redirecting output with System.out in Java:
Output redirection involves changing the default output stream using System.setOut()
.
PrintStream originalOut = System.out; // Save original output stream System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.out.println("This goes to the file."); System.setOut(originalOut); // Restore original output stream
Accessing system properties with System.getProperty():
System.getProperty()
retrieves the value of the specified system property.
String javaVersion = System.getProperty("java.version"); System.out.println("Java version: " + javaVersion);
Setting system properties in Java with System.setProperty():
System.setProperty()
allows setting a system property.
System.setProperty("my.property", "property-value");
Java System.arraycopy() method:
System.arraycopy()
is used to efficiently copy data between arrays.
int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = new int[5]; System.arraycopy(sourceArray, 0, destinationArray, 0, 5);
Managing security with System.getSecurityManager():
System.getSecurityManager()
returns the current security manager.
SecurityManager securityManager = System.getSecurityManager();
Using System.exit() in Java programs:
System.exit()
terminates the currently running Java Virtual Machine (JVM).
if (errorCondition) { System.err.println("Error occurred."); System.exit(1); // Terminate with a non-zero status code }
Java System.gc() for garbage collection:
System.gc()
is a hint to the garbage collector to perform garbage collection.
System.gc();
Working with standard input and output using System.in and System.out:
System.in
and System.out
are used for reading from and writing to the standard input and output streams.
Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); System.out.println("You entered: " + number);
Java System.nanoTime() for precise timing:
System.nanoTime()
provides a high-resolution time stamp, often used for precise timing measurements.
long startTime = System.nanoTime(); // Code to be timed long endTime = System.nanoTime(); long elapsedTime = endTime - startTime;
Java System.getenv() for environment variables:
System.getenv()
retrieves the value of the specified environment variable.
String userHome = System.getenv("HOME"); System.out.println("User's home directory: " + userHome);