Javatpoint Logo
Javatpoint Logo

print() Vs. println() in Java

Many techniques are available in Java, a popular object-oriented programming language, for delivering data to the console. Two methods that are frequently used for this are print() and println(). Even while they might look the same, there are a few subtle variations that could change how your output is shown. We will examine the differences between print and println in Java in this section.

1. print():

In Java, the print method displays text on the console without shifting the cursor to the next line. It just prints the text while keeping the pointer on the same line. The following is the syntax:

2. println():

Conversely, println() represents "print line." This function inserts a newline character (n) after printing the text, making it comparable to print() function. The pointer is advanced to the beginning of the next line. The following is the syntax:

When to Use print() method?

1. Formatting Output:

Print is frequently used when you want to concatenate or prepare output on the same line in a certain way.

2. Avoiding Extra Newlines:

Print is more appropriate in situations where you don't want to introduce extra newlines, such as when constructing a formatted output on the same line.

When to Use println() function?

1. Separating Lines:

When you want each output to appear on a new line, you can use println to make the output more legible. As an example

2. Readability and Structure:

When you have multiple statements and want to structure your output for better readability, println helps in organizing information on separate lines.

Mixing print() and println():

You can also mix print and println within the same code to achieve a desired format. For example:

Output:

Java C
Good Morning

print() Vs. println()

Feature print() Method println() Method
Line Termination Does not add a newline character at the end Adds a newline character at the end (\n)
Cursor Position Stays at the end of the printed text Moves to the beginning of the next line
Example System.out.print("Hello"); System.out.println("Hello");
Concatenation Concatenates multiple prints on the same line Starts a new line for each println statement
Usage Suitable for printing on the same line Suitable for printing on separate lines

Let's take a look at a small Java programme that contrasts the print and println methods:

PrintVsPrintln.java

Output:

Using print() method
Programming, Language
Using println() method
Programming, 
Language

The print() method is used in this example to print "Prohramming, " and "Language" on the same line. The println() method, on the other hand, prints "Programming" and "Language" on different lines by appending a newline character after each statement.

Advanced Formatting with printf():

In addition to print() and println(), Java includes the printf() method for more sophisticated output formatting. With the printf() method, you may customise the output's look by passing in format specifiers, including field width or the amount of decimal places for a floating-point integer.

Conclusion

In conclusion, the desired output format influences the decision between print and println in Java. If you wish to start a new line after printing, use println; otherwise, use print to retain the cursor on the current line. Knowing these differences can help you write Java applications that provide console output that is easier to understand and maintain. The intended output formatting determines whether to use print or println in Java. Use print to concatenate many text strings on a single line. Use println to have each statement display on its own line. Comprehending these distinctions enables developers to efficiently prepare console output according to their particular needs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA