-
Hajipur, Bihar, 844101
In every programming language, showing output to the user is one of the most basic and essential tasks. In Java, you’ll often need to display text, numbers, or calculation results on the screen. The Java output system is straightforward, but understanding how it works behind the scenes helps you write clearer and more flexible programs.
This tutorial explains how to print output in Java using the System.out object, the difference between print() and println(), how to format output, and how to handle concatenation with variables and data.
In Java, all output is handled through the standard output stream, represented by System.out. The System class is a part of the java.lang package and is automatically imported into every Java program.
You don’t need to create or import anything to use it. The out is a static member of the System class, which refers to an instance of PrintStream—a class responsible for printing text to the console.
So when you write:
System.out.println("Hello, World!");
You’re actually calling the println() method of the PrintStream class.
Java provides two common methods for displaying output:
print() – Prints text without moving to a new line.
println() – Prints text and then moves the cursor to the next line.
Let’s understand the difference with an example.
class PrintExample {
public static void main(String[] args) {
System.out.print("Welcome ");
System.out.print("to Java");
System.out.println();
System.out.println("Learning Output Methods");
}
}
Output:
Welcome to Java
Learning Output Methods
Here, print() keeps printing on the same line, while println() adds a new line after printing. You can use them together depending on how you want your output formatted.
You can easily print variables along with text by using the + operator for string concatenation.
class StudentDetails {
public static void main(String[] args) {
String name = "Ananya";
int age = 21;
double marks = 92.5;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
}
Output:
Name: Ananya
Age: 21
Marks: 92.5
The + operator joins strings with variables and converts other data types (like numbers) to strings automatically.
If you want multiple items on the same line, you can use multiple print() statements:
System.out.print("Java ");
System.out.print("is ");
System.out.print("powerful!");
Output:
Java is powerful!
This is useful when you want fine control over the layout of your output, such as when displaying formatted tables or progress indicators.
Sometimes, you’ll need to format your text with tabs, new lines, or special characters. Java supports escape sequences for that purpose.
Here are the most commonly used ones:
| Escape Sequence | Description | Example Output |
|---|---|---|
\n |
Inserts a new line | Moves text to the next line |
\t |
Inserts a tab space | Adds horizontal space |
\" |
Prints a double quote | Displays quotes in text |
\\ |
Prints a backslash | Shows \ in output |
Example:
System.out.println("Name:\tRiya");
System.out.println("City:\tMumbai");
System.out.println("Quote: \"Java is fun!\"");
Output:
Name: Riya
City: Mumbai
Quote: "Java is fun!"
When you want more control over how your output looks (for example, aligning numbers or setting decimal precision), Java provides the printf() method.
This method works similarly to C-style formatting and belongs to the same PrintStream class.
Syntax:
System.out.printf(format, arguments);
Example:
class FormatExample {
public static void main(String[] args) {
String product = "Notebook";
int quantity = 5;
double price = 45.6789;
System.out.printf("Product: %s\n", product);
System.out.printf("Quantity: %d\n", quantity);
System.out.printf("Price: %.2f\n", price);
}
}
Output:
Product: Notebook
Quantity: 5
Price: 45.68
Format specifiers:
| Specifier | Description |
|---|---|
%d |
Integer |
%f |
Floating-point number |
%s |
String |
%c |
Character |
%.2f |
Floating number rounded to 2 decimal places |
Using printf() makes it easier to control spacing, decimals, and alignment when working with numbers or tables.
You can also print multiple values in one statement by concatenating them.
Example:
int a = 10, b = 20;
System.out.println("Sum = " + (a + b));
Output:
Sum = 30
If you forget the parentheses, the + operator will act as a string joiner, not an arithmetic operator.
Example:
System.out.println("Sum = " + a + b);
Output:
Sum = 1020
This happens because Java first joins "Sum = " and a, turning everything into a string. Always use parentheses for arithmetic inside print statements.
Output methods are often used with loops to display repetitive data.
Example:
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
Output:
1 2 3 4 5
This prints all numbers on a single line. To print each number on a new line, replace print() with println().
To display special symbols like quotes, slashes, or percentage signs, use escape sequences properly:
System.out.println("She said, \"I love Java!\"");
System.out.println("Path: C:\\Program Files\\Java");
System.out.printf("Progress: 100%% Complete");
Output:
She said, "I love Java!"
Path: C:\Program Files\Java
Progress: 100% Complete
Missing Semicolon:
Every statement must end with ;. Forgetting it will cause a compile-time error.
Using Print() instead of print():
Method names are case-sensitive. Java will not recognize incorrect capitalization.
Incorrect String Quotes:
Strings must always be in double quotes. Single quotes are for characters.
Unclosed Parentheses:
Ensure all opening brackets ( and braces { are properly closed.
class OutputDemo {
public static void main(String[] args) {
System.out.println("=== Java Output Demo ===");
System.out.print("Name: ");
System.out.println("Kavya");
int age = 19;
double marks = 87.55;
System.out.printf("Age: %d\n", age);
System.out.printf("Marks: %.1f\n", marks);
System.out.println("Path Example: C:\\Java\\Program");
System.out.println("Quote Example: \"Practice makes perfect!\"");
}
}
Output:
=== Java Output Demo ===
Name: Kavya
Age: 19
Marks: 87.6
Path Example: C:\Java\Program
Quote Example: "Practice makes perfect!"
In this chapter, you learned how to display output in Java using different methods like print(), println(), and printf(). You also learned about string concatenation, escape sequences, and formatted output for precise display. Printing output is one of the simplest yet most important parts of Java programming because it helps you test and understand what your code is doing.
In the next tutorial, we’ll move on to Java Comments, where you’ll learn how to document your code effectively using single-line, multi-line, and documentation comments.
What is the purpose of System.out in Java?
What is the difference between print() and println() methods?
How does the + operator work when printing text and variables together?
What is an escape sequence in Java? Give two examples.
How does the printf() method differ from println()?
What are format specifiers in Java? Mention at least three commonly used ones.
What happens if you forget to use parentheses when performing arithmetic inside a print statement?
How can you print a double quote and backslash in Java output?
What is the use of the %f format specifier, and how can you control decimal precision?
What common errors should you avoid when writing output statements in Java?