String Formatting in Fortress

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 several printing methods for formatting general Java values.
        // For example, this prints an instance of our Point class.
        Point p = new Point(1, 2);
        System.out.printf("struct1: %s%n", p);

        // If you want to include the field names, you need to override toString() in the Point class.
        // For this example, we'll use reflection to demonstrate a similar output.
        System.out.printf("struct2: %s%n", String.format("{x:%d y:%d}", p.x, p.y));

        // To print a Java syntax representation of the value, you can use
        // the getClass() method along with the field values.
        System.out.printf("struct3: %s%n", p.getClass().getSimpleName() + "{x:" + p.x + ", y:" + p.y + "}");

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

        // Formatting booleans is straightforward.
        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, you need to escape the quotes.
        System.out.printf("str2: \"%s\"%n", "\"string\"");

        // To print a representation of a pointer, use System.identityHashCode().
        System.out.printf("pointer: %d%n", System.identityHashCode(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");

        // So far we've seen printf, which prints the formatted string to System.out.
        // 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 outputs using Formatter.
        Formatter formatter = new Formatter(System.err);
        formatter.format("io: an %s%n", "error");
        formatter.close();
    }
}

This Java code demonstrates various string formatting techniques, mirroring the functionality shown in the original Go example. Here are some key points about the Java version:

  1. Java uses System.out.printf() or String.format() for formatted output, which is similar to Go’s fmt.Printf() and fmt.Sprintf().

  2. The format specifiers in Java are very similar to those in Go, with some minor differences:

    • Java uses %n for newline instead of \n in format strings.
    • Java doesn’t have a %#v verb for Go-syntax representation, so we simulate it using reflection.
  3. Java doesn’t have built-in binary string representation for integers, so we use Integer.toBinaryString().

  4. Java doesn’t have a direct equivalent to Go’s pointer printing, so we use System.identityHashCode() to get a unique identifier for an object.

  5. For struct-like formatting, we use a custom Point class. To get field names in the output, you would typically override toString(), but here we use reflection-like approach for demonstration.

  6. Java’s Formatter class is used to format strings to other outputs, similar to Go’s Fprintf.

This code provides a comprehensive overview of string formatting in Java, covering various data types and formatting options.