{"id":2662,"date":"2016-06-24T10:44:06","date_gmt":"2016-06-24T10:44:06","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2662"},"modified":"2025-03-06T16:14:51","modified_gmt":"2025-03-06T16:14:51","slug":"difference-between-print-and-printf-statements","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/difference-between-print-and-printf-statements\/","title":{"rendered":"Difference between print() and printf() Methods"},"content":{"rendered":"<h1>Difference between print() and printf()<\/h1>\n<p>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.<\/p>\n<p>We can find these methods in the <strong>PrintStream<\/strong> class.<\/p>\n<h2>print() Method<\/h2>\n<p>The<strong> print()<\/strong> 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.<\/p>\n<p><strong>Method signature :<\/strong><\/p>\n<p><strong>public void print(String s)<\/strong><\/p>\n<p>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&#8217;s output.<\/p>\n<p>Ex: int, long, String, Object etc.<\/p>\n<p>Usage:<\/p>\n<p><em>System.out.print(&#8220;String to be printed&#8221;);<\/em><\/p>\n<p>The print() method does not add a line break after printing. You have to manually add a newline character (<strong>\\n)<\/strong> if you want it.\u00a0 There is a variation of this method called <strong>println(). <\/strong>This method prints and then terminates the current line. The print statements that come after this method print on a new line.<\/p>\n<p>Usage:<\/p>\n<p><em>System.out.println(&#8220;String to be printed&#8221;);<\/em><\/p>\n<h2>printf() Method<\/h2>\n<p>The printf() method is used for formatted output, where you can specify the format of the output using format specifiers (like<strong> %d<\/strong>, <strong>%s<\/strong>, <strong>%f<\/strong>, etc.).<br \/>\nWhen you use printf(), you can specify how the output should look (like controlling decimal places, padding, etc.)<\/p>\n<p>&nbsp;<\/p>\n<p><em>public PrintStream printf(String format, Object &#8230; args)<\/em><\/p>\n<p><strong>Usage:<\/strong><\/p>\n<p><em>System.out.printf( \u201cFormat-string\u201d [, arg1, arg2, &#8230; ] );<\/em><\/p>\n<p>It does <strong data-start=\"1215\" data-end=\"1222\">not<\/strong> add a newline by default, just like<code class=\"\" data-line=\"\">print()<\/code>, but you can add one manually (<code class=\"\" data-line=\"\">\\n<\/code>) if needed. The difference is that it allows for <strong data-start=\"1354\" data-end=\"1370\">more control<\/strong> over how data is displayed.<\/p>\n<h2>print() vs printf()<\/h2>\n<table border=\"1\">\n<thead>\n<tr>\n<th>Aspect<\/th>\n<th>print()<\/th>\n<th>printf()<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Purpose<\/td>\n<td>Used to print content to the console without a newline at the end.<\/td>\n<td>Used to print formatted output with control over the layout of the printed content.<\/td>\n<\/tr>\n<tr>\n<td>Line Break<\/td>\n<td>Does not automatically add a newline. You must manually add `\\n` if needed.<\/td>\n<td>Does not automatically add a newline. You must manually add `\\n` or use format specifiers for line breaks.<\/td>\n<\/tr>\n<tr>\n<td>Formatting<\/td>\n<td>No formatting control. Prints data as-is.<\/td>\n<td>Supports format specifiers like `%d`, `%s`, `%f`, etc., for formatting the output.<\/td>\n<\/tr>\n<tr>\n<td>Usage<\/td>\n<td>Simple output when formatting is not required.<\/td>\n<td>Used when you need precise control over how the output is displayed (e.g., controlling number of decimal places, padding, etc.).<\/td>\n<\/tr>\n<tr>\n<td>Example<\/td>\n<td><code class=\"\" data-line=\"\">System.out.print(&quot;Hello&quot;);<\/code><\/td>\n<td><code class=\"\" data-line=\"\">System.out.printf(&quot;Hello, %s! You have %d messages.&quot;, &quot;Alice&quot;, 5);<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><\/h2>\n<h2>Java Program<\/h2>\n<p>&nbsp;<\/p>\n<pre>package com.testingdocs.java.tutorials;\r\n\/\/www.TestingDocs.com\r\n\/\/print() and printf() methods demo\r\n\r\npublic class JavaDemo {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tString str = \"Hello World\"; \r\n\t\tdouble fmtTwoDecimalsValue=0.072345;\r\n\t\tSystem.out.println(str);\r\n\t\tSystem.out.print(\"Hello \");\r\n\t\tSystem.out.print(\"World\");\r\n\t\tSystem.out.println();\r\n\t\tSystem.out.printf(\"Value =: %.2f\",fmtTwoDecimalsValue);\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n<h3>Program Output<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-18768 size-full\" title=\"Difference between print() and printf() Statements\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/print-and-println-output.png\" alt=\"Difference between print() and printf() Statements\" width=\"1346\" height=\"934\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/print-and-println-output.png 1346w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/print-and-println-output-300x208.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/print-and-println-output-1024x711.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/print-and-println-output-768x533.png 768w\" sizes=\"auto, (max-width: 1346px) 100vw, 1346px\" \/><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li data-start=\"2358\" data-end=\"2426\">Use <code class=\"\" data-line=\"\">print()<\/code> for simple output without formatting or line breaks.<\/li>\n<li data-start=\"2427\" data-end=\"2533\" data-is-last-node=\"\">Use <code class=\"\" data-line=\"\">printf()<\/code> for more advanced, formatted output where you need control over how values are displayed.<\/li>\n<\/ul>\n<h2>Java Tutorials<\/h2>\n<p>Java Tutorial on this website:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Difference between print() and printf() Statements : This tutorial will examine some differences between print and print statements. We can find these methods in the PrintStream class.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-2662","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2662"}],"version-history":[{"count":23,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2662\/revisions"}],"predecessor-version":[{"id":27018,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2662\/revisions\/27018"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}