Skip to Content
bytegarden

Printing

  • Java provides a built-in class called System. System has a statc object called Out. Out is an object of PrintStream class.
  • 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

  1. print() - prints without a new line

  2. println() - prints and moves cursor to next line

  3. printf() - similar to C language’s printf

  4. format() - for formatted output (similar to printf())

    • Returns a Formatter or String
    • More flexibility for creating formatted strings
  5. See print examples (Print.java, below)

    Print.java
    public 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

On this page