Difference between print() and printf() Methods
Difference between print() and printf()
This tutorial will examine some differences between print and print statements in the Java language. In Java, the print() and printf() methods are both used for displaying output, but they work in different ways.
We can find these methods in the PrintStream class.
print() Method
The print() method prints the given content to the console and does not add a newline after the output. When you use it, whatever you print stays on the same line.
Method signature :
public void print(String s)
This method accepts a single value of any of the primitive or reference data types as a parameter and prints the given value as the program’s output.
Ex: int, long, String, Object etc.
Usage:
System.out.print(“String to be printed”);
The print() method does not add a line break after printing. You have to manually add a newline character (\n) if you want it. There is a variation of this method called println(). This method prints and then terminates the current line. The print statements that come after this method print on a new line.
Usage:
System.out.println(“String to be printed”);
printf() Method
The printf() method is used for formatted output, where you can specify the format of the output using format specifiers (like %d, %s, %f, etc.).
When you use printf(), you can specify how the output should look (like controlling decimal places, padding, etc.)
public PrintStream printf(String format, Object … args)
Usage:
System.out.printf( “Format-string” [, arg1, arg2, … ] );
It does not add a newline by default, just likeprint()
, but you can add one manually (\n
) if needed. The difference is that it allows for more control over how data is displayed.
print() vs printf()
Aspect | print() | printf() |
---|---|---|
Purpose | Used to print content to the console without a newline at the end. | Used to print formatted output with control over the layout of the printed content. |
Line Break | Does not automatically add a newline. You must manually add `\n` if needed. | Does not automatically add a newline. You must manually add `\n` or use format specifiers for line breaks. |
Formatting | No formatting control. Prints data as-is. | Supports format specifiers like `%d`, `%s`, `%f`, etc., for formatting the output. |
Usage | Simple output when formatting is not required. | Used when you need precise control over how the output is displayed (e.g., controlling number of decimal places, padding, etc.). |
Example | System.out.print("Hello"); |
System.out.printf("Hello, %s! You have %d messages.", "Alice", 5); |
Java Program
package com.testingdocs.java.tutorials; //www.TestingDocs.com //print() and printf() methods demo public class JavaDemo { public static void main(String[] args) { String str = "Hello World"; double fmtTwoDecimalsValue=0.072345; System.out.println(str); System.out.print("Hello "); System.out.print("World"); System.out.println(); System.out.printf("Value =: %.2f",fmtTwoDecimalsValue); } }
Program Output
- Use
print()
for simple output without formatting or line breaks. - Use
printf()
for more advanced, formatted output where you need control over how values are displayed.
Java Tutorials
Java Tutorial on this website: