What is the Difference Between print and println

The main difference between print and println is that print method prints the string but does not move the cursor to a new line while println method prints the string and moves the cursor to a new line.

Java is a general-purpose, high-level programming language. It is used to build web, desktop, mobile and other distributed applications. It is a platform independent language and provides many features such as automatic garbage collection and support for multithreading. Furthermore, the Java library provides classes with various methods. The programmer can use them in his program. Two of them are print and println. In overall, they both help to print a text on the console, but the position of the cursor after printing the text is different.   

Key Areas Covered

1. What is print
     – Definition, Functionality
2. What is println
    – Definition, Functionality
3. Difference Between print and println
    – Comparison of Key Differences

Key Terms

Java, Method, print, println

Difference Between print and println - Comparison Summary

What is print

The print is a method in Java that helps to display a text on the console. The programmer has to pass the required String as a parameter to the print method. Then, this method prints the text on the console. After printing, the cursor remains at the end of that text in the console. The new text continues printing from there.

Some example print methods are as follows.

void print(int s) – It helps to print an integer.

void print(String s) – This function is used to print a string.

void print (Boolean b) – It helps in printing Boolean values.

What is println

Similar to print method, println is also a method available in Java that helps to display a text on the console, but after printing the text, the cursor moves into the next line at the console. Therefore, the other new text appears on a new line. Moreover, the programmer has to pass the text as a String to this method.

Some example print methods are as follows.

void println() – It terminates the current line by writing the line separator string.

void println(int x) – This function prints an integer and then terminates the line.

void println(String x) – This method prints a String and then, makes the cursor to appear in a new line.

void println(Boolean x) – It prints a Boolean and then terminates the line.

Refer to the below example.

Difference Between print and println

Figure 1: Java program with print and println

In the above program, the print method displays “Hello” on the console. After printing it, the cursor remains at the end on that text. In other words, the cursor does not go to the new line. Again there is a print method with “World”. It prints after the String “Hello”. After print “World”, the cursor remains in the same position. In line 8, there is a println method it prints “Hello” String and moves the cursor to the next line. Finally, the next println method prints the String “World” on the next line.

Difference Between print and println

Definition

The print is a method in Java that is used to display a piece of text on the console and allows the cursor to remain in the same position. In contrast, println is a method in Java that is used to display a piece of text on the console and allows the cursor to go to the new line. Hence, this is the main difference between print and println.

Cursor

In the print method, the cursor remains on the same line after printing the text while in the println method, the cursor moves to the next line after printing the text.

Arguments

Moreover, another difference between print and println is that the print method only works with arguments. Otherwise, it will cause a syntax error. On the other hand, the println method can work without arguments.

Conclusion

The print and println are two methods available in Java, and the programmer can use these methods according to the requirement in the program. The main difference between print and println is that print method prints the string but does not move the cursor to a new line while the println method prints the string and moves the cursor to a new line. In brief, print does not add any new line while println adds a new line after displaying the text.

Reference:

1. Java Basic Input and Output, Available here.

About the Author: Lakna

Lakna, a graduate in Molecular Biology and Biochemistry, is a Molecular Biologist and has a broad and keen interest in the discovery of nature related things. She has a keen interest in writing articles regarding science.

Leave a Reply