0
0
JavaHow-ToBeginner · 3 min read

How to Print Output in Java: Simple Guide with Examples

In Java, you print output using System.out.println() to print text followed by a new line, or System.out.print() to print text without a new line. These methods send text to the console so you can see results while your program runs.
📐

Syntax

The basic syntax to print output in Java uses the System.out object with two common methods:

  • System.out.println(value); prints value and moves to a new line.
  • System.out.print(value); prints value but stays on the same line.

Here, value can be text (in quotes), numbers, or variables.

java
System.out.println("Hello, world!");
System.out.print("Hello, ");
System.out.print("world!");
Output
Hello, world! Hello, world!
💻

Example

This example shows how to print text and numbers using both println and print. It demonstrates the difference in line breaks.

java
public class PrintExample {
    public static void main(String[] args) {
        System.out.println("Welcome to Java printing!");
        System.out.print("This is on the same line, ");
        System.out.print("right after this.");
        System.out.println(); // prints a new line
        System.out.println(123); // prints a number
    }
}
Output
Welcome to Java printing! This is on the same line, right after this. 123
⚠️

Common Pitfalls

Beginners often forget to add quotes around text, causing errors. Another mistake is mixing print and println without understanding line breaks, which can make output confusing.

Also, using System.out.println without parentheses or semicolon causes syntax errors.

java
/* Wrong: Missing quotes around text */
// System.out.println(Hello world); // Error

/* Right: Text in quotes */
System.out.println("Hello world");

/* Wrong: Forgetting semicolon */
// System.out.println("Hello"); // Correct
// System.out.println("Hello")  // Error

/* Mixing print and println */
System.out.print("Hello");
System.out.println(" World");
Output
Hello World
📊

Quick Reference

Use this quick guide to remember how to print output in Java:

MethodDescriptionExample
System.out.println()Prints text and moves to a new lineSystem.out.println("Hi");
System.out.print()Prints text without moving to a new lineSystem.out.print("Hi");
System.out.printf()Prints formatted text (like placeholders)System.out.printf("Number: %d", 5);

Key Takeaways

Use System.out.println() to print text with a new line.
Use System.out.print() to print text without a new line.
Always put text inside double quotes when printing strings.
Remember to end statements with a semicolon (;).
Mix print and println carefully to control output formatting.