Java System Methods

Built-in system operations and utilities

⚙️ What are System Methods?

System methods in Java provide access to system-level operations like printing output, getting current time, copying arrays, and managing program execution efficiently.


// Basic System methods
System.out.println("Hello World!");
long currentTime = System.currentTimeMillis();
System.out.println("Current time: " + currentTime);
                                    

Common System Methods

🖨️

System.out

Print output to console

System.out.println("Hello!");
System.out.print("No newline");

currentTimeMillis()

Get current system time

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

arraycopy()

Copy arrays efficiently

int[] source = {1, 2, 3};
int[] dest = new int[3];
System.arraycopy(source, 0, dest, 0, 3);
🚪

exit()

Terminate program execution

System.exit(0); // Normal exit
System.exit(1); // Error exit

🔹 Output Methods

Different ways to print output:

public class OutputExample {
    public static void main(String[] args) {
        // println - adds new line after printing
        System.out.println("This is line 1");
        System.out.println("This is line 2");
        
        // print - no new line
        System.out.print("Hello ");
        System.out.print("World!");
        System.out.println(); // Add new line
        
        // printf - formatted output
        String name = "Alice";
        int age = 25;
        double height = 5.6;
        
        System.out.printf("Name: %s, Age: %d, Height: %.1f%n", name, age, height);
        
        // Printing variables
        int score = 95;
        System.out.println("Your score is: " + score);
        
        // Printing calculations
        System.out.println("10 + 5 = " + (10 + 5));
    }
}

Output:

This is line 1
This is line 2
Hello World!
Name: Alice, Age: 25, Height: 5.6
Your score is: 95
10 + 5 = 15

🔹 Time and Performance

Measuring time and program performance:

public class TimeExample {
    public static void main(String[] args) {
        // Get current time in milliseconds
        long startTime = System.currentTimeMillis();
        
        System.out.println("Current time (milliseconds): " + startTime);
        
        // Simulate some work
        for (int i = 0; i < 1000000; i++) {
            // Some calculation
            Math.sqrt(i);
        }
        
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        
        System.out.println("Operation took: " + duration + " milliseconds");
        
        // Get time in nanoseconds for more precision
        long nanoStart = System.nanoTime();
        
        // Quick operation
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            sum += i;
        }
        
        long nanoEnd = System.nanoTime();
        long nanoDuration = nanoEnd - nanoStart;
        
        System.out.println("Sum calculation took: " + nanoDuration + " nanoseconds");
        System.out.println("Sum result: " + sum);
    }
}

Sample Output:

Current time (milliseconds): 1640995200000
Operation took: 45 milliseconds
Sum calculation took: 125000 nanoseconds
Sum result: 499500

🔹 Array Copy Example

Efficiently copying arrays using System.arraycopy():

public class ArrayCopyExample {
    public static void main(String[] args) {
        // Original array
        int[] originalArray = {10, 20, 30, 40, 50};
        
        // Create destination array
        int[] copiedArray = new int[5];
        
        // Copy entire array
        System.arraycopy(originalArray, 0, copiedArray, 0, originalArray.length);
        
        System.out.println("Original array:");
        printArray(originalArray);
        
        System.out.println("Copied array:");
        printArray(copiedArray);
        
        // Partial copy example
        int[] partialCopy = new int[3];
        System.arraycopy(originalArray, 1, partialCopy, 0, 3); // Copy elements 1,2,3
        
        System.out.println("Partial copy (elements 1-3):");
        printArray(partialCopy);
        
        // String array example
        String[] names = {"Alice", "Bob", "Charlie", "Diana"};
        String[] namesCopy = new String[4];
        
        System.arraycopy(names, 0, namesCopy, 0, names.length);
        
        System.out.println("String array copy:");
        for (String name : namesCopy) {
            System.out.print(name + " ");
        }
        System.out.println();
    }
    
    public static void printArray(int[] array) {
        for (int value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}

Output:

Original array:
10 20 30 40 50
Copied array:
10 20 30 40 50
Partial copy (elements 1-3):
20 30 40
String array copy:
Alice Bob Charlie Diana

🔹 System Properties

Getting system information:

public class SystemPropertiesExample {
    public static void main(String[] args) {
        // Get system properties
        System.out.println("Java Version: " + System.getProperty("java.version"));
        System.out.println("Operating System: " + System.getProperty("os.name"));
        System.out.println("User Name: " + System.getProperty("user.name"));
        System.out.println("User Home: " + System.getProperty("user.home"));
        System.out.println("Current Directory: " + System.getProperty("user.dir"));
        
        // Get environment variables
        String javaHome = System.getenv("JAVA_HOME");
        if (javaHome != null) {
            System.out.println("JAVA_HOME: " + javaHome);
        } else {
            System.out.println("JAVA_HOME not set");
        }
        
        // Memory information
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long usedMemory = totalMemory - freeMemory;
        
        System.out.println("\nMemory Information:");
        System.out.println("Total Memory: " + (totalMemory / 1024 / 1024) + " MB");
        System.out.println("Used Memory: " + (usedMemory / 1024 / 1024) + " MB");
        System.out.println("Free Memory: " + (freeMemory / 1024 / 1024) + " MB");
    }
}

Sample Output:

Java Version: 11.0.2
Operating System: Windows 10
User Name: john
User Home: C:\Users\john
Current Directory: C:\Projects\JavaApp
JAVA_HOME: C:\Program Files\Java\jdk-11.0.2

Memory Information:
Total Memory: 245 MB
Used Memory: 12 MB
Free Memory: 233 MB

🧠 Test Your Knowledge

Which method is used to print output with a new line?