Print
System.out.print
- Print string without changing a new line
class Output
{
public static void main(String args[])
{
System.out.print("Hello ");// not change a new line
System.out.print("World!\n");
}
}
System.out.println
- Print string with changing a new line
class Output
{
public static void main(String args[])
{
System.out.println("Hello ");
System.out.println("World!");
}
}
System.out.printf, System.out.format
- printf and format are equivalent
class Output
{
public static void main(String args[])
{
//Decimal
System.out.printf("%d%n", 10000);// print an integer
System.out.printf("%10d%n", 10000);// print an integer with width 10, defalut is right-justified
System.out.printf("%-10d%n", 10000);// left-justified
System.out.printf("%,10d%n", 10000);// locale-specific grouping characters
//Float
System.out.printf("%f%n", Math.PI);// print a float number
System.out.printf("%10.2f%n", Math.PI);// print a float number with width 10 and 2 significant figure
System.out.printf("%-10.2f%n", Math.PI);// left-justified
System.out.printf("%e%n", 12345678.0);// print a float number in exponential notation
System.out.printf("%g%n", 12345678.0);// print a float number in either the floating-point format f or the exponential format e based on the magnitude of the value
System.out.printf("%a%n", 12345678.0);// print a float number in hexadecimal format
//Octal
System.out.printf("%o%n", 10);
//Hexadecimal
System.out.format("%x%n", 10);
//Character
System.out.format("%c%n", 'A');
//String
System.out.format("%s%n", "String");
}
}
Print Date and Time
import java.util.Calendar;
class Output
{
public static void main(String args[])
{
Calendar c = Calendar.getInstance();
// Date/Time Compositions
System.out.format("%tc%n", c);// tc, date
System.out.format("%tF%n", c);// tF, date
System.out.format("%tr%n", c);// tr
System.out.format("%tT%n", c);// tT
//Date
System.out.format("%1$tA, %1$tB %1$td, %1$tY%n", c);// argument index to indicate all format specifiers use the first argument
//Time
System.out.format("%1$tH:%1$tM:%1$tS%n", c);
}
}
Other Conversion Characters
class Output
{
public static void main(String args[])
{
System.out.format("%b%n", false);// boolean
System.out.format("%h%n", "Hello");// hash code
System.out.format("%%%n");// %
System.out.format("%n");// new line
}
}
Argument Indices
class Output
{
public static void main(String args[])
{
System.out.format("%4$s %3$s %2$s %1$s%n", "One", "Two", "Three", "Four");
}
}
Escape
- \', single quote
- \", double quote
- \\, backslash character
- \b, backspace
- \f, new page
- \n, new line
- \r, carriage return
- \t, horizontal tab
DecimalFormat
import java.text.DecimalFormat;
public class Output{
static public void customFormat(String pattern, double value ) {
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);
}
static public void main(String[] args) {
customFormat("###,###.###", 123456.789);
customFormat("###.##", 123456.789);
customFormat("000000.000", 123.78);
customFormat("$###,###.###", 12345.67);
}
}
Reference