Java

Java Syntax for print() and println()

In Java, the outcomes play a vital role in analyzing the implemented functionalities properly. For instance, printing the values in one go or with a line break. In such situations, Java provides the “print()” and “println()” methods that can be utilized to analyze the code from time to time and display the values differently.

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

System.out.print()
System.out.println()

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:

public class printlnandprint {
 public static void main(String args[]) {
  Integer x = 3;
  char y = 'J';
  String z = "Linuxhint";
  System.out.print("The integer value is: "+x);
  System.out.print("The character value is: "+y);
  System.out.print("The string value is: "+z); 
}}

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:

public class printlnandprint {
 public static void main(String args[]) {
  Integer x = 3;
  char y = 'J';
  String z = "Linuxhint";
  System.out.println("The integer value is: "+x);
  System.out.println("The character value is: "+y);
  System.out.println("The string value is: "+z);   
}}

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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.