This blog will elaborate on the usage and implementation of the “print()” and “println()” methods in Java.
What are the “print()” and “println()” Methods in Java?
The “print()” method prints the particular values without any line break whereas the “println()” method prints the values with a default line break.
Note: To make the message readable in the former method, place an additional “\n”.
Syntax
Here, “println()” is a short form of “print line“.
In the above syntax, “System” refers to a built-in Java class that accumulates useful members, such as out, which is short for “output“.
Example 1: Applying “print()” in Java to Display the Values Comprising Multiple Data Types
In this example, the “print()” method can be applied to print the initialized “Integer”, “character”, and “String” type values:
In the above code snippet, initialize the stated values comprising the “Integer”, “char”, and “String” data types and print these values via the “print()” method.
Output
In this output, it can be seen that the printed values are displayed side by side without any line break.
To display the values properly with a line break, place an additional “\n” instead, as follows:
As observed, the outcome of each value is readable now.
Example 2: Applying “println()” in Java to Display the Values Comprising Multiple Data Types
In this particular example, the “println()” method can be utilized to display the values of multiple data types:
In the above lines of code, recall the discussed approaches for initializing the stated values comprising the specified data types and print these values with a default line break.
Output
According to the given output, it can be analyzed that the initialized values are printed with a default line break.
Conclusion
In Java, the “print()” method prints the particular values without any line break whereas the “println()” method prints the values with a default line break. To achieve the outcome randomly without any formatting, the former method can be used. However, to make the code readable with default line spacing, the latter method can be considered. This blog guided the usage and difference between the “print()” and “println()” methods in Java.