Printing Text Output
2 min read ·
Printing text output is the most basic and important operation in Java. It allows a program to display messages, information, and results on the screen.
Using System.out.println
The most commonly used method for printing output in Java is
System.out.println.This statement prints the text and then moves the cursor to the next line.
Printing Multiple Lines of Text
You can print multiple lines by writing multiple
println statements.Each line is printed on a new line in the output.
Printing Text Exactly as Written
Java prints text exactly as it appears inside double quotes.
Text inside double quotes is called a string literal.
Note
If you forget double quotes, Java will treat the text as code and show an error.
Using System.out.print
The
System.out.print method prints text without moving to a new line.The output will appear on the same line.
When to Use print or println
Use
println when you want output on a new line.
Use print when you want output to continue on the same line.Common Mistakes While Printing Text
Missing double quotes around text
Forgetting the semicolon at the end
Writing code outside the main method
Caution
Java output statements must always be written inside a class and the main method.
Why Printing Text Is Important
Printing output helps you:
Understand program flow
Debug your code
Display results to users
Goal Achieved
You can now correctly print text output in Java using proper and runnable code.