Print in Java with Examples

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Programming languages use print statements, which allow the output of a problem to be displayed to the console or terminal. These Java print statements give information about the behavior of the program and the values of variables at certain locations in the code, which is crucial for debugging. The concept of print statements will be examined in this article using examples in the Java programming language.

How to Print in Java?

The println() in Java is one of the methods that is widely used by users to print a sentence or the output to the console. This method belongs to the PrintStream class of Java. However, there are other methods as well like print() and printf() that can be used to print the output to the console with a minor difference in each method.

The type of print statement in Java that one should use depends on what the output is or what is to be printed to the console. Let us discuss each of the Java printing methods one by one to clearly understand the difference between each one of them.

The print() method in Java is used to print text on the console or the terminal. It accepts a string as a parameter. It also belongs to the PrintStream class of Java language.

The key point in this method is that after printing the output or the text on the console, the cursor remains on the same line which means that anything that is going to be printed next will also appear on the same line.

Syntax

The syntax for the print() method in Java is as follows:

As you can see in the above syntax, we have used the System.out which is a Java method to create an instance of the PrintStream class. The PrintStream class cannot be used directly, therefore there is a need for System.out standard output stream.

  • System - System refers to a final class that is defined in Java.lang package which is used to display data.
  • out - It is an instance of the System class in Java which is used to create objects internally in Java.

Parameters

The print() method of Java takes a string as a parameter and displays the output given to it. However, if the parameter passed to the function as an argument is null, it prints the string null.

Whenever a string is passed as a parameter to the print() method, the characters of the string are converted to bytes based on the default character encoding in Java. This method also works if nothing is passed to it.

Returns

The print() method in Java does not return anything. It just displays the output passed to it as a parameter. Therefore, it has a return type of void.

Overloads of Print() Method in Java

The overloaded methods of the print() method in Java are as follows:

Overloaded methodDescription
print(boolean b)It prints a boolean value, that is, true or false.
print(double d)It prints a double value to the console.
print(long l)It prints a long integer value to the console.
print(float f)It prints a floating value to the console.
print(int i)It prints an integer value to the console.
print(object obj)It prints an object to the console.
print(char c)It prints a character value to the console.
print(char[] s)It prints an array of characters to the console.

Examples

Now, let us look at a code example to understand the print() method in Java.

Output:

As you can in the above code example, all three text "Welcome" is printed on the same line. If you want to shift them in the next line, we should be using the escape character \n.

Println() Method in Java

It is an upgraded version of the print() method. It also belongs to the PrintStream class of Java. The main difference between the print() and println() methods in Java is that the println() method places the cursor at the start of the next line after printing the output. On the other hand, the print() method places the cursor on the same line after printing on the console.

The println() in Java also accepts a string as a parameter and displays the text on the console or the terminal.

Syntax

The syntax of the println() statement in Java is as follows:

As you can see in the above syntax, the System and out is also used with the println() method. System.out is the standard output stream in which System is a final class and out refers to the static member of the System class.

Parameters

Similar to the print() method in Java, it also takes a string as a parameter and displays it to the console. It also converts the characters of the string passed to the function into bytes using the default character encoding in Java.

Moreover, it also works when nothing is passed to it, it then displays an empty string.

Returns

The return type of the println() method in Java is also void as it does not return anything. It just prints the result on the console whatever is passed to it as a parameter.

Overloads of PrintIn() Method in Java

The overloaded methods of println() method in Java are as follows:

Overloaded methodDescription
println(boolean b)It prints a boolean value, that is, true or false.
println(double d)It prints a double value to the console.
println(long l)It prints a long integer value to the console.
println(float f)It prints a floating value to the console.
println(int i)It prints an integer value to the console.
println(object obj)It prints an object to the console.
println(char c)It prints a character value to the console.
println(char[] s)It prints an array of characters to the console.

Let's see a code example in which we will be using the above-overloaded functions to understand them better.

Output:

As you can see in the above code example, we have used some of the overloaded functions like boolean, int, double, and char. As we give the values to the println() method, they are printed to the console.

Examples

Now, let us see a code example to clearly understand the println() method in Java.

Output:

As you can see in the above code example, we have used the println() method to print the strings. All three strings are displayed on different lines because of the effect of the println() method.

Use Cases

It is used when the user is required to print the results on different lines.

Printf() Method in Java

The printf() method in Java is used to print the formatted string to the console using the given format string and arguments. It belongs to the PrintStream class and has multiple overloads. The method functions in the same way as calling the format() method.

Syntax

The syntax of the println() statement is as follows:

In the above syntax, System.out refers to the standard output stream.

  • format - It refers to the formatting specifier like %s, %d, etc.
  • data - It is a parameter referenced by the format specifiers. The additional arguments are not taken into consideration if the number of arguments exceeds the format specifiers.

Parameters

It takes two parameters - one is the format specifier which is the type of text that is to be printed whereas the second is the data which is the actual text to be printed to the console.

If the format is null, it throws the NullPointerException.

However, if a format string has illegal syntax, it also throws the IllegalFormatException.

Returns

It returns the output stream. Therefore, it has a return type of PrintStream.

Overloads of Printf() Method in Java

The overloads of the printf() method in Java are as follows:

printf(Locale l, String format, Object... args): It writes a formatted string to this output stream using the formatted string and arguments.

Examples

Common Example Explaining All the Three

Let us now understand all three methods with the help of a code example.

Output:

As you can see in the above code example, we have implemented all three methods print(), println(), and printf().

FAQs

Q. What's the difference between System.out.print() and System.out.println()?

A. System.out.print() prints output and makes the cursor stay at the same line, so subsequent output appears on the same line. System.out.println() adds a newline character, moving the output to the next line.

Q. What is the purpose of print statements in Java programming?

A. Print statements in Java are used to display output on the console or the terminal. They help debug and track the behavior of your program by showing values of variables and intermediate results during execution.

Q. How can I format output using the printf() method in Java?

A. The printf() method provides formatting using format specifiers. For example, %d is used for integers, %f for floating-point numbers, and %s for strings.

Conclusion

  • Print statements are used to display output to the console or the terminal.
  • These different print statements in Java are present in the PrintStream class.
  • The println() method gets the cursor to a new line so the next output prints on a new line.
  • We need to write the print methods along with the System.out standard output stream.
  • The print() method makes the stay on the same line, therefore the subsequent out stays at the same line.
  • The printf() method is used for formatting the strings using the format specifiers.