Other methods for comparing subparts of strings or ignoring case differences are also covered in Chapter 13.
If you use == to compare the string objects, you are actually comparing oneStr and twoStr to see if they
refer to the same object, not testing if the strings have the same contents.
Exercise 1.11: Modify the StringsDemo application to use different strings.
Exercise 1.12: Modify ImprovedFibonacci to store the String objects it creates into an array instead
of invoking println with them directly.
1.10.1. String Conversion and Formatting
The concatenation operator converts primitive values to strings using the toString method of the
corresponding wrapper class. This conversion doesn't give you any control over the format of the resulting
string. Formatted conversion can be done with the java.util.Formatter class. A Formatter can
write its output to a string, a file, or some other output device. For convenience the System.out.printf
method (for "print formatted") uses a formatter to write to System.out. Formatted output uses a format
string together with a set of values to format. The format string contains normal text together with format
specifiers that tell the formatter how you want the subsequent values to be formatted. For example, you can
print the value of Math.PI to three decimal places using
System.out.printf("The value of Math.PI is %.3f %n", Math.PI);
which prints
The value of Math.PI is 3.142
whereas using println and string concatenation you'd get:
The value of Math.PI is 3.141592653589793
A format specifier consists of at least two parts: it starts with a % character, and it ends with a conversion
indicator. The conversion identifies both the type of the value to format, and the basic form. For example, %f
says to format a floating-point value in the usual decimal form, as used for Math.PI above, whereas %e says
to format a floating point value in scientific notationfor example, 3.142e+00. Integer values can be formatted
by %d for normal decimal, or %x for hexadecimal form. A string can be formatted by %s.
The %n conversion causes insertion of the correct line-separator character, something that println does
automatically for you. The line-separator depends on the current platform and may not be a simple \n
(newline) character. If you are familiar with printf from the C programming language you need to get used
to using %n instead of \n.
A format specifier can provide additional formatting information. You can provide a width that indicates the
minimum number of characters to printuseful for aligning columns of data. If the formatted value has fewer
characters than the width, it is padded with spaces to fit the minimum size. This lets you align values easily.
Some conversions also allow a precision value to be given, which is written as. followed by a non-negative
number. For floating-point values using %f the precision indicates how many decimal places to round toin the
example above the value of Math.PI is rounded to 3 decimal places because of the .3 in the format
specifier. If both a width and precision are given they are written in the form width.precision.