How do I format strings with String::formatted?

The String::formatted method in Java is a concise way to format strings using placeholders, similar to the String::format method but with a cleaner syntax. It was introduced in Java 15, and it allows you to replace placeholders in a string with specified values.

The syntax of the formatted method is straightforward:

String formattedString = "Your name is %s and your age is %d".formatted("John", 25);

Here’s how it works:

  1. The placeholders in the string (such as %s, %d) follow the same format specifiers as used in String.format().
    • %s: Formats strings.
    • %d: Formats integers.
    • %f: Formats floating-point numbers.
    • And so on.
  2. The formatted() method takes the format arguments in the exact order of appearance of the placeholders.

Example Usage:

Here are a few examples illustrating the different use cases:

Example 1: Format a simple text

String result = "Hello, %s!".formatted("Alice");
System.out.println(result);
// Output: Hello, Alice!

Example 2: Combine multiple placeholders

String summary = "Product: %s, Quantity: %d, Price: $%.2f".formatted("Widget", 10, 9.99);
System.out.println(summary);
// Output: Product: Widget, Quantity: 10, Price: $9.99

Example 3: Use with text blocks

In text blocks, you can similarly use the formatted method to insert values dynamically:

String jsonTemplate = """
    {
        "name": "%s",
        "age": %d,
        "email": "%s"
    }
    """;

String json = jsonTemplate.formatted("John", 30, "[email protected]");
System.out.println(json);
// Output:
// {
//     "name": "John",
//     "age": 30,
//     "email": "[email protected]"
// }

Key Points:

  • The formatted method is directly callable on the string you want to format, making the code cleaner.
  • It has the same capabilities as String.format, so it supports all format specifiers.
  • formatted works particularly well with text blocks for clean and readable multi-line string formatting.

This method is helpful for writing concise and fluent code without the need to call String.format explicitly.

How to Use System.currentTimeMillis() for Performance Timing

In Java, System.currentTimeMillis() is commonly used as a simple way to measure the execution time of a block of code or a specific operation in terms of milliseconds. Here’s how you can effectively use it for performance timing:

Example Usage

package org.kodejava.lang;

public class PerformanceTimingExample {
    public static void main(String[] args) {
        // Record the start time
        long startTime = System.currentTimeMillis();

        // The code you want to measure
        performOperation();

        // Record the end time
        long endTime = System.currentTimeMillis();

        // Calculate the elapsed time
        long elapsedTime = endTime - startTime;

        // Print the result
        System.out.println("Execution time: " + elapsedTime + " milliseconds");
    }

    private static void performOperation() {
        try {
            // Simulate time-consuming task
            Thread.sleep(2000); // Sleep for 2 seconds
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Steps Explained

  1. Record Start Time: Use System.currentTimeMillis() before the block of code you want to measure.
  2. Execute Operation: Run the code or process whose performance you need to measure.
  3. Record End Time: Capture the time after the code execution using System.currentTimeMillis().
  4. Calculate Elapsed Time: Subtract the start time from the end time to get the elapsed time in milliseconds.
  5. Output Results: Display or log the elapsed time for performance analysis.

Things to Keep in Mind

  • Resolution: System.currentTimeMillis() measures the current time in milliseconds since the Unix epoch (January 1, 1970). However, its granularity may vary depending on the system, and it is not as precise as System.nanoTime() for very fine-grained measurements.
  • Avoid Garbage Collection Interference: When measuring performance, ensure that garbage collection has minimal impact by warming up the JVM and avoiding memory-intensive operations.
  • Use System.nanoTime() for Better Precision: If you need higher precision or want to avoid timer granularity issues, consider using System.nanoTime() instead. This measures elapsed time in nanoseconds and is suitable for shorter durations.

Example with System.nanoTime()

package org.kodejava.lang;

public class NanoTimingExample {
    public static void main(String[] args) {
        // Record the start time
        long startTime = System.nanoTime();

        // The code you want to measure
        performOperation();

        // Record the end time
        long endTime = System.nanoTime();

        // Calculate the elapsed time in milliseconds
        long elapsedTime = (endTime - startTime) / 1_000_000;

        // Print the result
        System.out.println("Execution time: " + elapsedTime + " milliseconds");
    }

    private static void performOperation() {
        try {
            // Simulate time-consuming task
            Thread.sleep(2000); // Sleep for 2 seconds
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

Conclusion

System.currentTimeMillis() is a simple and effective method to time operations, especially those involving multiple seconds or milliseconds. However, for finer-grained timing or benchmarking (e.g., sub-millisecond accuracy), prefer System.nanoTime(). Always ensure that your measurements are consistent and unaffected by other system activities, such as garbage collection or OS-level processes.

How to Use StringBuilder for Efficient String Concatenation

In Java, using StringBuilder is a common way to handle efficient string concatenation, especially when working with loops or when you need to concatenate a large number of strings. Unlike String, which is immutable, StringBuilder is mutable and modifies its internal character array without creating new objects, hence improving performance.

Here’s how you can use StringBuilder for efficient string concatenation:

1. Creating a StringBuilder instance

You can create a new instance of StringBuilder using its constructor:

StringBuilder sb = new StringBuilder();

You can also initialize it with an existing string:

StringBuilder sb = new StringBuilder("Hello");

2. Appending Strings

Use the .append() method to concatenate strings:

StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
System.out.println(sb.toString()); // Output: "Hello World"

Here, the append() method modifies the existing StringBuilder instance.


3. Inserting Strings

To insert a string at a specific position, use the .insert() method:

StringBuilder sb = new StringBuilder("Hello World");
sb.insert(6, "Beautiful ");
System.out.println(sb.toString()); // Output: "Hello Beautiful World"

4. Replacing Part of the String

You can replace part of the string using .replace():

StringBuilder sb = new StringBuilder("Hello Java");
sb.replace(6, 10, "World");
System.out.println(sb.toString()); // Output: "Hello World"

5. Reversing the String

You can reverse the string using .reverse():

StringBuilder sb = new StringBuilder("abcd");
sb.reverse();
System.out.println(sb.toString()); // Output: "dcba"

6. Deleting Characters or Substrings

You can use .delete() or .deleteCharAt() to remove parts of the string:

StringBuilder sb = new StringBuilder("Hello World");
sb.delete(5, 11); // Remove characters from index 5 to 10
System.out.println(sb.toString()); // Output: "Hello"

sb.deleteCharAt(0); // Remove the character at index 0
System.out.println(sb.toString()); // Output: "ello"

7. Converting Back to a String

Once you are done building the string, convert it back to a String using .toString():

StringBuilder sb = new StringBuilder("Hello");
String result = sb.toString();
System.out.println(result); // Output: "Hello"

8. StringBuilder in Loops

It is particularly useful when appending strings in loops to avoid the overhead of creating multiple String instances:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
    sb.append("Number ").append(i).append(", ");
}
System.out.println(sb.toString());
// Output: "Number 0, Number 1, Number 2, Number 3, Number 4, "

Example: Complete Code

Here’s a complete example that combines multiple methods:

public class StringBuilderExample {
    public static void main(String[] args) {
        // Create a StringBuilder
        StringBuilder sb = new StringBuilder("Example");

        // Append strings
        sb.append(" of").append(" StringBuilder");

        // Insert a string
        sb.insert(8, " java");

        // Replace a substring
        sb.replace(0, 7, "Demo");

        // Delete part of the string
        sb.delete(5, 10);

        // Reverse the string
        sb.reverse();

        // Convert back to String
        System.out.println(sb.toString());
    }
}

Output:

redliuBgnirtS fo omeD

Performance Comparison: String vs StringBuilder

Here’s a quick comparison of the performance:

  • String: Creates a new object for each concatenation, which is inefficient in loops.
  • StringBuilder: Reuses the same object and modifies its internal buffer, which is much faster.

So, whenever you’re performing a lot of string manipulations, especially in loops, it’s highly recommended to use StringBuilder.

How to Format Strings Using String.format()

In Java, the String.format() method is a convenient way to create formatted strings using placeholders. It allows you to include values such as numbers or strings at specific positions in a string by using format specifiers. Here’s how you can use it:

Syntax

String.format(String format, Object... args)
  • format: The format string with placeholders.
  • args: The arguments to replace the placeholders.

Common Format Specifiers

  • %s: String.
  • %d: Decimal integer.
  • %f: Floating-point number.
  • %c: Character.
  • %%: Literal % character.

You can combine these with width, precision, alignment, and other formatting options.


Examples

1. String Formatting

String name = "John";
int age = 30;
String formattedString = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(formattedString);
// Output: My name is John and I am 30 years old.

2. Formatting Numbers

double price = 123.456789;
String formattedPrice = String.format("The price is %.2f.", price);
System.out.println(formattedPrice);
// Output: The price is 123.46.
  • %.2f: Limits the floating-point value to 2 decimal places.

3. Padding and Alignment

  • Right-aligned text:
String formattedString = String.format("%10s", "Java");
System.out.println(formattedString);
// Output: "      Java" (padded with spaces to the left, 10 characters in total)
  • Left-aligned text:
String formattedString = String.format("%-10s", "Java");
System.out.println(formattedString);
// Output: "Java      " (padded with spaces to the right, 10 characters in total)

4. Adding Leading Zeros

int number = 42;
String formattedNumber = String.format("%05d", number);
System.out.println(formattedNumber);
// Output: 00042

5. Formatting Multiple Values

String result = String.format("%s scored %d out of %d in the exam.", "Alice", 90, 100);
System.out.println(result);
// Output: Alice scored 90 out of 100 in the exam.

6. Escaping %

To include a literal % in the string, use %%.

String formattedString = String.format("Progress: %.2f%%", 85.123);
System.out.println(formattedString);
// Output: Progress: 85.12%

Notes

  1. Null Values: If a value in args is null, %s outputs the string "null".
  2. Exceptions: Make sure the placeholders match the number and type of arguments; otherwise, it may throw an exception (e.g., IllegalFormatException).

Formatted strings are especially useful when generating user-friendly messages or handling precise output formatting, such as in reporting systems or logs.

How do I get operating system process information using ProcessHandle?

Java 9 introduced the ProcessHandle API, which allows us to interact with and retrieve information about native processes. Here’s how we can use ProcessHandle to get information about operating system processes:

We can list all the processes currently running on the system and print their details:

package org.kodejava.example.lang;

import java.time.Duration;
import java.time.Instant;

public class ProcessHandleExample {
    public static void main(String[] params) {
        ProcessHandle.allProcesses()
                .forEach(process -> {
                    long pid = process.pid();
                    ProcessHandle.Info info = process.info();
                    String cmd = info.command().orElse("");
                    String[] args = info.arguments().orElse(new String[0]);
                    Instant startTime = info.startInstant().orElse(null);
                    Duration cpuUsage = info.totalCpuDuration().orElse(Duration.ZERO);

                    System.out.println("PID        = " + pid);
                    System.out.println("Command    = " + cmd);
                    System.out.println("Args       = " + String.join(" ", args));
                    System.out.println("Start Time = " + startTime);
                    System.out.println("CPU Usage  = " + cpuUsage);
                    System.out.println("------------");
                });
    }
}

If we want to get information about a specific process, we can use their process ID (PID):

package org.kodejava.example.lang;

import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

public class SpecificProcessInfo {
    public static void main(String[] params) {
        // Replace with the PID of the process you want to query
        long pid = 33656;

        // Get the ProcessHandle of the specific process
        Optional<ProcessHandle> processHandle = ProcessHandle.of(pid);
        if (processHandle.isPresent()) {
            ProcessHandle process = processHandle.get();
            pid = process.pid();
            ProcessHandle.Info info = process.info();
            String cmd = info.command().orElse("");
            String[] args = info.arguments().orElse(new String[0]);
            Instant startTime = info.startInstant().orElse(null);
            Duration cpuUsage = info.totalCpuDuration().orElse(Duration.ZERO);

            System.out.println("PID        = " + pid);
            System.out.println("Command    = " + cmd);
            System.out.println("Args       = " + String.join(" ", args));
            System.out.println("Start Time = " + startTime);
            System.out.println("CPU Usage  = " + cpuUsage);
            System.out.println("------------");
        } else {
            System.out.println("No process found with PID: " + pid);
        }
    }
}

Output:

PID        = 33656
Command    = C:\Users\wayan\AppData\Local\Programs\IntelliJ IDEA Ultimate\bin\idea64.exe
Args       = 
Start Time = 2024-07-22T03:14:07.825Z
CPU Usage  = PT46M27.484375S
------------

Explanation

  • ProcessHandle.allProcesses(): returns a stream of all processes currently running on the system.
  • ProcessHandle.of(pid): returns an Optional<ProcessHandle> for the process with the given PID.
  • ProcessHandle.Info: contains information about a process, such as its command, arguments, start time, and CPU usage.
  • info.command(): returns an Optional<String> with the command used to start the process.
  • info.arguments(): returns an Optional<String[]> with the arguments passed to the process.
  • info.startInstant(): returns an Optional<Instant> with the start time of the process.
  • info.totalCpuDuration(): returns an Optional<Duration> with the total CPU time used by the process.

Using the ProcessHandle API in Java 9 and later makes it straightforward to get detailed information about operating system processes.