Java Output / Print

The output of a program is an essential aspect of software development, as it provides feedback to the user about what the program is doing. Java provides several ways to output data to the user.

This guide will cover the basic methods of printing in Java, including the System.out.print() and System.out.println() methods, as well as the printf() method.

System.out.print() and System.out.println()

The System.out.print() and System.out.println() methods are the most basic and frequently used methods for printing output in Java.

The difference between the two is that the println() method adds a newline character after the text, while the print() method does not.


System.out.println("Hello World!");
System.out.print("Hello World!");


Hello World!
Hello World!

Printing Variables

Variables can also be printed in Java by concatenating them with the text that you want to display.


int num = 10;
System.out.println("The number is " + num);


The number is 10

printf() Method

The printf() method is used to print output with formatted text.

The basic syntax of the printf() method is:

System.out.printf("Format string", argument list);

The format string specifies the format of the output, and the argument list provides the values that should be displayed.


System.out.printf("%d", 10);
System.out.printf("%.2f", 10.12345);
System.out.printf("%s", "Hello World");


Hello World

The above example demonstrates the use of several format specifiers: %d for printing an integer, %.2f for printing a floating-point number with two decimal places, and %s for printing a string.


In this guide, we have covered the basics of printing output in Java, including the System.out.print() and System.out.println() methods and the printf() method.

Whether you are a beginner or an experienced Java developer, knowing how to print output is an essential skill for any software developer.

Practice these methods and explore their capabilities to get the most out of your Java programs.

Related Posts: