/** * FormattedStrings.java * Jeff Ondich, Carleton College, 2014-01-05 * * Demonstrates string formatting. * * This is the Java half of a pair of parallel examples in Python and Java. * See formattedstrings.py. */ public class FormattedStrings { public static void main(String[] args) { int n = 701; float x = 3.0f / 7.0f; String animal = "gerenuk"; String announcement1 = String.format("The animal: %s", animal); System.out.println(announcement1); String announcement2 = String.format("The integer: %d (decimal), %X (hexadecimal)", n, n); System.out.println(announcement2); // Note that in Java, you can skip String.format and just call format on // System.out if you don't need to store the resulting string. System.out.format("The real number with 2, then 3 digits past the decimal point: %.2f, %.3f\n", x, x); } }