Table of contents
1.
Introduction
2.
What is `print` in Java?  
2.1.
Syntax of `print`  
2.2.
Example of `print`
2.3.
When to Use `print`  
3.
What is `println` in Java?  
3.1.
Syntax of `println`  
3.2.
Example of `println` in Action  
3.3.
When to Use `println`  
3.4.
Should I use print or println in my code?  
4.
Key Differences Between print() and println()
5.
Example Comparing print() and println()
6.
When to Use print() and println()?
7.
Frequently Asked Questions
7.1.
What happens if I mix print() and println() in my Java program?
7.2.
Can I use print() to display numbers and variables?
7.3.
What is the best way to display a formatted table in Java?
8.
Conclusion
Last Updated: Mar 4, 2025
Easy

Difference Between print and println in Java

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, print and println are methods of the System.out class used to display output on the console. The key difference is that print outputs text without a newline, while println appends a newline after printing the text. This means print keeps the cursor on the same line, whereas println moves it to the next line. 

Difference Between print and println in Java

In this article, we will learn their differences with examples.

What is `print` in Java?  

The `print ` method in Java is used to display text or data on the console without moving the cursor to the next line. It is part of the `System` class, which is included in the `java.lang` package. This means you don’t need to import anything extra to use it. The `print ` method simply outputs whatever you pass to it & keeps the cursor on the same line, ready for the next output.  

Syntax of `print`  

The syntax for the `print` method is very easy:  

System.print(data);


Here, `data` can be a string, number, variable, or even an expression. The method will convert the data into a string & display it on the console.  

Example of `print`

Let’s look at a simple example to understand how `print` works:  

public class PrintExample {
    public static void main(String[] args) {
        System.out.print("Hello, ");
        System.out.print("World!");
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code:   

1. We start by creating a class named `PrintExample`.  
 

2. Inside the `main` method, we use `System.out.print("Hello, ");` to display the text "Hello, " on the console. Notice that the cursor stays on the same line after this output.  
 

3. Next, we use `System.out.print("World!");` to display "World!" on the same line as "Hello, ".  
 

When you run this program, the output will look like this:  

Hello, World!


As you can see, both `print ` statements combine their output on a single line. This is the key behavior of the `print` method.  

When to Use `print`  

  • Use `print ` when you want to display multiple pieces of data on the same line.  
     
  • It’s useful for creating formatted output or combining text with variables without adding unnecessary line breaks.  


For example, if you want to display a user’s name & age on the same line, you can use `print` like this:  

String name = "Alice";
int age = 25;
System.out.print("Name: " + name + ", ");
System.out.print("Age: " + age);
You can also try this code with Online Java Compiler
Run Code


Output:  

Name: Alice, Age: 25


This makes your output cleaner & easier to read.  

What is `println` in Java?  

The `println` method in Java is used to display text or data on the console & then move the cursor to the next line. Like `print`, it is part of the `System` class in the `java.lang` package. The key difference is that `println` adds a line break after the output, making it ideal for displaying separate lines of text or data.  

Syntax of `println`  

The syntax for the `println` method is similar to `print `:  

System.out.println(data);


Here, `data` can be a string, number, variable, or expression. After displaying the data, `println` moves the cursor to the next line.  

Example of `println` in Action  

Let’s look at a simple example to understand how `println` works:  

public class PrintlnExample {
    public static void main(String[] args) {
        System.out.println("Hello, ");
        System.out.println("World!");
    }
}
You can also try this code with Online Java Compiler
Run Code


In this Code: 

1. We create a class named `PrintlnExample`.  
 

2. Inside the `main` method, we use `System.out.println("Hello, ");` to display the text "Hello, " on the console. After this output, the cursor moves to the next line.  
 

3. Next, we use `System.out.println("World!");` to display "World!" on a new line.  


When you run this program, the output will look like this:  

Hello, 
World!


As you can see, each `println` statement places its output on a new line. This is the key behavior of the `println` method.  

When to Use `println`  

  • Use `println` when you want to display each piece of data on a new line.  
     
  • It’s useful for creating readable & well-structured output, especially when displaying lists or multiple lines of text.  


For example, if you want to display a list of items, you can use `println` like this:  

System.out.println("Shopping List:");
System.out.println("1. Apples");
System.out.println("2. Bread");
System.out.println("3. Milk");
You can also try this code with Online Java Compiler
Run Code


Output:  

Shopping List:
1. Apples
2. Bread
3. Milk


This makes your output organized & easy to follow.  

Should I use print or println in my code?  

Use `print` when you want to display output on the same line, like combining text or variables without line breaks. Use `println` when you want each output to appear on a new line, making it ideal for lists or structured displays. Choose based on how you want your output to look.  

Key Differences Between print() and println()

Featureprint()println()
Line BreakDoes not move to a new lineMoves to a new line after printing
Cursor PositionStays on the same lineMoves to the next line after printing
UsageUsed when continuous output is neededUsed when separate lines of output are required

Example Comparing print() and println()

public class PrintVsPrintln {
    public static void main(String[] args) {
        System.out.print("Hello ");
        System.out.print("Java ");
        System.out.println("World!");
        System.out.print("Coding ");
        System.out.println("Ninjas");
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Hello Java World!
Coding Ninjas

 

Explanation:

  • print("Hello ") and print("Java ") stay on the same line.
     
  • println("World!") moves to the next line after printing "World!".
     
  • print("Coding ") stays on the same line, but println("Ninjas") moves to the next line after printing "Ninjas".

When to Use print() and println()?

SituationBest Method to Use
Printing multiple values on the same lineprint()
Displaying formatted output with proper spacingprintln()
Creating structured output with separate linesprintln()
Printing a progress indicator or status updateprint()

Frequently Asked Questions

What happens if I mix print() and println() in my Java program?

Mixing print() and println() results in some outputs appearing on the same line and others moving to new lines.

Can I use print() to display numbers and variables?

Yes, print() can display numbers and variables along with text in a continuous format.

What is the best way to display a formatted table in Java?

Using println() ensures each row appears on a new line, making the table more readable.

Conclusion

In this article, we discussed the difference between print and println in Java. The print method prints output on the same line without a newline, while println prints the output followed by a new line. Understanding this difference helps in formatting console output properly and improving the readability of Java programs.

Live masterclass