

{"id":126054,"date":"2024-08-13T18:00:11","date_gmt":"2024-08-13T12:30:11","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=126054"},"modified":"2024-08-13T18:14:28","modified_gmt":"2024-08-13T12:44:28","slug":"java-string-format-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/","title":{"rendered":"Java String format() Method with Examples"},"content":{"rendered":"<p>The format() method in Java is an essential tool for creating formatted strings by combining a format string with specific arguments. It provides a mechanism to format these arguments within a string according to a specified format.<\/p>\n<p>The primary purpose of the format() method is to offer a versatile way to concatenate strings with other variables or values while maintaining a structured and organized format. Instead of manually concatenating strings, you can employ format specifiers within the format string, which will be seamlessly substituted with the provided arguments.<\/p>\n<h2>Syntax of Java String format()<\/h2>\n<p><strong>The syntax of the two format() methods is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ With locale\r\npublic static String format(Locale loc, String format, Object... args)\r\n\r\n\/\/ Without locale \r\npublic static String format(String format, Object... args)<\/pre>\n<p><strong>The parameters are:<\/strong><\/p>\n<ul>\n<li><strong>loc:<\/strong> The locale used for formatting (optional).<\/li>\n<li><strong>format:<\/strong> The format string.<\/li>\n<li><strong>args:<\/strong> The arguments to replace format specifiers.<\/li>\n<\/ul>\n<p>This method returns the formatted string.<\/p>\n<p><strong>It throws:<\/strong><\/p>\n<ul>\n<li><strong>NullPointerException:<\/strong> If the format string is null<\/li>\n<li><strong>IllegalFormatException:<\/strong> If format string is invalid or arguments do not match<\/li>\n<\/ul>\n<h3>Java Format Specifiers<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Format Specifier<\/b><\/td>\n<td><b>Data Type<\/b><\/td>\n<td><b>Output or Return value<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%a<\/span><\/td>\n<td><span style=\"font-weight: 400\">floating point<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns a Hex output of floating point number<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%b<\/span><\/td>\n<td><span style=\"font-weight: 400\">Any type<\/span><\/td>\n<td><span style=\"font-weight: 400\">True or False<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%c<\/span><\/td>\n<td><span style=\"font-weight: 400\">character<\/span><\/td>\n<td><span style=\"font-weight: 400\">Unicode character<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%d<\/span><\/td>\n<td><span style=\"font-weight: 400\">integer<\/span><\/td>\n<td><span style=\"font-weight: 400\">Decimal Integer<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%e<\/span><\/td>\n<td><span style=\"font-weight: 400\">floating point<\/span><\/td>\n<td><span style=\"font-weight: 400\">A decimal number in scientific notation<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%f<\/span><\/td>\n<td><span style=\"font-weight: 400\">floating point<\/span><\/td>\n<td><span style=\"font-weight: 400\">Decimal number<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%g<\/span><\/td>\n<td><span style=\"font-weight: 400\">floating point<\/span><\/td>\n<td><span style=\"font-weight: 400\">Decimal number, possibly in scientific notation, depending on the precision and value<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%h<\/span><\/td>\n<td><span style=\"font-weight: 400\">Any type<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hex String of value from hashCode() method<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%n<\/span><\/td>\n<td><span style=\"font-weight: 400\">None<\/span><\/td>\n<td><span style=\"font-weight: 400\">Platform-specific line separator<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%o<\/span><\/td>\n<td><span style=\"font-weight: 400\">integer<\/span><\/td>\n<td><span style=\"font-weight: 400\">Octal number<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%s<\/span><\/td>\n<td><span style=\"font-weight: 400\">Any type<\/span><\/td>\n<td><span style=\"font-weight: 400\">String value<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%t<\/span><\/td>\n<td><span style=\"font-weight: 400\">Date\/Time<\/span><\/td>\n<td><span style=\"font-weight: 400\">%t is the prefix for Date\/Time conversions.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%x<\/span><\/td>\n<td><span style=\"font-weight: 400\">integer<\/span><\/td>\n<td><span style=\"font-weight: 400\">Hex string<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Examples<\/h3>\n<h4>Example 1: Formatting Personal Information<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class IndianPersonalInfo {\r\n    public static void main(String[] args) {\r\n        String name = \"Rajesh\";\r\n        int age = 28;\r\n        String city = \"Mumbai\";\r\n\r\n        String formattedString = String.format(\"Name: %s, Age: %d, City: %s\", name, age, city);\r\n        System.out.println(formattedString);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nName: Rajesh, Age: 28, City: Mumbai<\/p>\n<h4>Example 2: Formatting Currency<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class IndianCurrency {\r\n    public static void main(String[] args) {\r\n        double price = 999.50;\r\n\r\n        String formattedPrice = String.format(\"Price: \u20b9%.2f\", price);\r\n        System.out.println(formattedPrice);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nPrice: \u20b9999.5<\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, the format() method in Java is a robust resource for shaping strings according to your specific requirements. With the aid of format specifiers, it grants you the flexibility to format arguments of diverse data types, simplifying string concatenation and enhancing readability.<\/p>\n<p>An array of predefined format specifiers and flags is available, enabling you to fine-tune the formatting, including aspects like padding, precision, sign, radix, and more. Mastery of the format() method and its various specifier types will undoubtedly empower you to craft well-structured strings, making data output a more efficient and effective process. We recommend practising with different specifier types to become proficient in utilizing the format() method to its fullest potential.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The format() method in Java is an essential tool for creating formatted strings by combining a format string with specific arguments. It provides a mechanism to format these arguments within a string according to&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":126074,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[7345,31171,31174,31078,8152,31173,31172],"class_list":["post-126054","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-format-method","tag-java-string-format-method-with-examples","tag-java-tutorials","tag-learn-java","tag-string-format-method","tag-string-format-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String format() Method with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"The format() method in Java stands as a robust resource for shaping strings in a manner that suits your specific requirements.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-string-format-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String format() Method with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The format() method in Java stands as a robust resource for shaping strings in a manner that suits your specific requirements.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-string-format-method\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-13T12:30:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-13T12:44:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-format.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String format() Method with Examples - DataFlair","description":"The format() method in Java stands as a robust resource for shaping strings in a manner that suits your specific requirements.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String format() Method with Examples - DataFlair","og_description":"The format() method in Java stands as a robust resource for shaping strings in a manner that suits your specific requirements.","og_url":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-08-13T12:30:11+00:00","article_modified_time":"2024-08-13T12:44:28+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-format.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java String format() Method with Examples","datePublished":"2024-08-13T12:30:11+00:00","dateModified":"2024-08-13T12:44:28+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/"},"wordCount":389,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-format.webp","keywords":["Java","java string format() method","java string format() method with examples","java tutorials","Learn Java","string format() method","string format() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-string-format-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/","url":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/","name":"Java String format() Method with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-format.webp","datePublished":"2024-08-13T12:30:11+00:00","dateModified":"2024-08-13T12:44:28+00:00","description":"The format() method in Java stands as a robust resource for shaping strings in a manner that suits your specific requirements.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-string-format-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-format.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-format.webp","width":1200,"height":628,"caption":"java string format()"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-string-format-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java String format() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126054","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=126054"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126054\/revisions"}],"predecessor-version":[{"id":143186,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126054\/revisions\/143186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/126074"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=126054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=126054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=126054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}