Printing
- Java provides a built-in class called
System.Systemhas a statc object calledOut.Outis an object ofPrintStreamclass. - The
println()method is overloaded to support different data types. (No separate methods for byte and short as they are treated as int)
Printing Methods
-
print()- prints without a new line -
println()- prints and moves cursor to next line -
printf()- similar to C language’s printf- Takes two main arguments: Format string; Variable arguments (varargs)
- See more info about printf()
-
format()- for formatted output (similar toprintf())- Returns a Formatter or String
- More flexibility for creating formatted strings
-
See print examples (
Print.java, below)Print.javapublic class Print { public static void main(String[] args) { int a = 1, b = 2, c = 3; System.out.println("Values: " + a + ", " + b + ", " + c); System.out.printf("Values: %d, %d, %d", a, b, c); System.out.format("Values: %d, %d, %d", a, b, c); } }Runs in your browser with Java 21. Ctrl/⌘ + Enter to run.Built with CheerpJ