MY mENU


Friday 10 February 2012

Format Strings in Java


The String Class has a method format(), that returns a String object rather than a PrintStream object.

Using String's static format() method allows you to create a formatted String that we can reuse, as opposed to a one-time print statement.

Example:
- instead of
System.out.printf("The value of the float variable is %f, while the value of the " + "integer variable is %d, and the string is %s", floatVar, intVar, stringVar);

- we can write
String fs;
fs = String.format("The value of the float variable is %f, while the value of the " + "integer variable is %d, and the string is %s", floatVar, intVar, stringVar);
System.out.println(fs);

No comments:

Post a Comment