Java print() vs println(): Console Output, Examples and Differences

0

Java provides several ways to display information in the console, but print() and println() are the two methods beginners use most often. They look almost identical, yet their behaviour is slightly different—and understanding that difference is essential for controlling how your output appears.

What Is print()?

The print() method displays the specified value in the console without automatically moving the cursor to the next line.

System.out.print("Hello");
System.out.print("Java");

The output is:

HelloJava

Both messages appear on the same line because the first print() does not create a new line after displaying Hello.

What Is println()?

The println() method displays the specified value and then moves the cursor to the next line.

System.out.println("Hello");
System.out.println("Java");

The output is:

Hello
Java

After the first println() finishes displaying Hello, the cursor moves to the next line. Therefore, Java appears below it.

print() vs println()

Method Moves to New Line? Example
print() No System.out.print("Hello");
println() Yes System.out.println("Hello");

The easiest way to remember the difference is: print() stays on the current line, while println() prints and then moves to the next line.

Using print() for User Prompts

The difference becomes particularly useful when taking input from the user. A prompt is often displayed using print() so that the user can enter the value on the same line.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.println("Hello " + name);
    }
}

The output may look like this:

Enter your name: Bibhu
Hello Bibhu

Using print() keeps the input on the same line as the question, making the interaction cleaner.

Using println() for Separate Messages

When displaying multiple independent pieces of information, println() is often more readable.

System.out.println("Student Details");
System.out.println("Name: Bibhu");
System.out.println("Age: 25");
System.out.println("Marks: 85.5");

Each statement produces a separate line, making the output easy to scan.

Combining Multiple print() Calls

Multiple print() statements can be combined to construct one line of output.

System.out.print("Name: ");
System.out.print("Bibhu");
System.out.print(", Age: ");
System.out.print(25);

The output is:

Name: Bibhu, Age: 25

This technique is useful when you want precise control over where spaces, values, and text appear.

Adding Spaces with print()

Java does not automatically insert spaces between separate print() calls.

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

The result is:

HelloWorld

If a space is required, it must be included explicitly.

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

Now the output becomes:

Hello World

Using an Empty println()

An empty println() can be used simply to move the cursor to the next line.

System.out.print("Hello");
System.out.println();
System.out.print("Java");

The output is:

Hello
Java

This is useful when you want to build part of a line with print() and then deliberately move to a new line.

Printing Variables

Both methods can display variables.

String name = "Bibhu";
int age = 25;

System.out.print(name);
System.out.println(age);

The output will be:

Bibhu25

If a separator is required, add it explicitly.

System.out.print(name + " ");
System.out.println(age);

Now the output is:

Bibhu 25

Printing Expressions

Both methods can also display the result of an expression.

int a = 10;
int b = 20;

System.out.print(a + b);

The result is:

30

When text is involved, pay attention to the difference between concatenation and arithmetic.

System.out.println("Total: " + 10 + 20);

This produces:

Total: 1020

Because the expression has already started as a String concatenation, the numbers are joined as text. To perform the addition first, use parentheses.

System.out.println("Total: " + (10 + 20));

Now the output is:

Total: 30

When combining text with calculations, use parentheses when you want the calculation to happen before String concatenation.

Common Beginner Mistakes

One common mistake is expecting print() to automatically create a new line.

System.out.print("Java");
System.out.print("Programming");

This produces JavaProgramming, not two separate lines.

Another mistake is forgetting that spaces must be explicitly included.

System.out.print("Java");
System.out.print(" Programming");

Now the output contains the intended space.

When Should You Use Each Method?

There is no rule that says one method is always better. The choice depends on how you want the output to appear.

Situation Recommended Method Reason
User prompt print() Keeps the cursor on the same line.
Separate message println() Moves to the next line automatically.
Building one line step by step print() Provides precise line control.
Displaying a list of results println() Each result can appear on its own line.

Interview Insight

If an interviewer asks for the difference between print() and println(), the essential answer is simple: print() writes the value without automatically moving to the next line, while println() writes the value and then terminates the current line.

A practical interviewer may then ask why print() is commonly used for input prompts. The reason is that it allows the user to enter their response on the same line as the prompt.

Quick Revision

Method Main Behaviour
print() Displays output and stays on the current line.
println() Displays output and moves to the next line.
println() Can also be used without an argument to create a new line.
+ Combines Strings and values or performs numeric addition.
Parentheses Can force calculations to happen before String concatenation.

The difference between print() and println() is small, but it gives you direct control over the presentation of console output. Once this becomes natural, you can create cleaner prompts, readable results, and well-structured Java console programs.

Post a Comment

0Comments
Post a Comment (0)