String Formatting in Miranda

import java.util.Formatter;

class Point {
    int x, y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

public class StringFormatting {
    public static void main(String[] args) {
        // Java offers excellent support for string formatting in
        // the printf tradition. Here are some examples of
        // common string formatting tasks.

        // Java uses printf for formatted output to console
        // and String.format for creating formatted strings.

        Point p = new Point(1, 2);
        System.out.printf("struct1: %s%n", p);

        // If the object has a meaningful toString method,
        // %s will include the object's string representation.
        System.out.printf("struct2: %s%n", p);

        // Java doesn't have a direct equivalent to Go's %#v,
        // but we can create a custom method to achieve similar results.
        System.out.printf("struct3: %s%n", String.format("new Point(%d, %d)", p.x, p.y));

        // To print the type of a value, use getClass().getSimpleName()
        System.out.printf("type: %s%n", p.getClass().getSimpleName());

        // Formatting booleans is straight-forward.
        System.out.printf("bool: %b%n", true);

        // There are many options for formatting integers.
        // Use %d for standard, base-10 formatting.
        System.out.printf("int: %d%n", 123);

        // This prints a binary representation.
        System.out.printf("bin: %s%n", Integer.toBinaryString(14));

        // This prints the character corresponding to the given integer.
        System.out.printf("char: %c%n", 33);

        // %x provides hex encoding.
        System.out.printf("hex: %x%n", 456);

        // There are also several formatting options for floats.
        // For basic decimal formatting use %f.
        System.out.printf("float1: %f%n", 78.9);

        // %e and %E format the float in (slightly different versions of) scientific notation.
        System.out.printf("float2: %e%n", 123400000.0);
        System.out.printf("float3: %E%n", 123400000.0);

        // For basic string printing use %s.
        System.out.printf("str1: %s%n", "\"string\"");

        // To double-quote strings, use %s with quotes in the format string.
        System.out.printf("str2: \"%s\"%n", "string");

        // As with integers seen earlier, we can use String.format to get a hex representation
        System.out.printf("str3: %s%n", String.format("%x", "hex this".getBytes()));

        // To print a representation of a reference, use %h.
        System.out.printf("reference: %h%n", p);

        // When formatting numbers you will often want to control the width and precision
        // of the resulting figure. To specify the width of an integer, use a number after
        // the % in the format specifier. By default the result will be right-justified and
        // padded with spaces.
        System.out.printf("width1: |%6d|%6d|%n", 12, 345);

        // You can also specify the width of printed floats, though usually you'll also want
        // to restrict the decimal precision at the same time with the width.precision syntax.
        System.out.printf("width2: |%6.2f|%6.2f|%n", 1.2, 3.45);

        // To left-justify, use the - flag.
        System.out.printf("width3: |%-6.2f|%-6.2f|%n", 1.2, 3.45);

        // You may also want to control width when formatting strings, especially to ensure
        // that they align in table-like output. For basic right-justified width.
        System.out.printf("width4: |%6s|%6s|%n", "foo", "b");

        // To left-justify use the - flag as with numbers.
        System.out.printf("width5: |%-6s|%-6s|%n", "foo", "b");

        // String.format formats and returns a string without printing it anywhere.
        String s = String.format("sprintf: a %s", "string");
        System.out.println(s);

        // You can format+print to other output streams using Formatter
        Formatter stderr = new Formatter(System.err);
        stderr.format("io: an %s%n", "error");
    }
}

To run this program, save it as StringFormatting.java, compile it with javac StringFormatting.java, and then run it with java StringFormatting. The output will be similar to the Go example, with slight differences due to Java’s specific formatting behavior.

This Java code demonstrates various string formatting techniques available in Java, which are similar to those in Go. The main differences are:

  1. Java uses System.out.printf() for console output instead of fmt.Printf().
  2. Java uses String.format() to create formatted strings, similar to Go’s fmt.Sprintf().
  3. Java doesn’t have a direct equivalent to Go’s %#v format specifier, but we can create custom methods to achieve similar results.
  4. Java uses %h to print a hash code of an object, which is somewhat similar to printing a pointer in Go.
  5. Java doesn’t have a built-in way to print binary representations of integers, so we use Integer.toBinaryString().

The concepts of width and precision in formatting, left and right justification, and various format specifiers for different types are very similar between Java and Go.