{"id":89386,"date":"2025-06-05T18:00:35","date_gmt":"2025-06-05T12:30:35","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89386"},"modified":"2025-06-05T11:29:53","modified_gmt":"2025-06-05T05:59:53","slug":"java-tostring-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/","title":{"rendered":"Java toString() Method"},"content":{"rendered":"<p>The toString() method returns the String representation of the object. However, the Java compiler internally invokes the toString() method on the object if you publish any object. So, booting the toString() system returns the requested object, which can be the state of an object, etc.<\/p>\n<h2>How to use Java toString ()method<\/h2>\n<p>This tutorial will teach us about the Java toString() method. We&#8217;ll examine the Description and programming of the toString() Java Method.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<p>After reading this tutorial, you will understand the concepts of the toString() Java method and be comfortable using it in your programs to get the String representation of the object.<\/p>\n<h3>Java ToString()<\/h3>\n<p>As the name suggests, the Java toString() method returns the String equivalent of the object that invokes it.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-ToString.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-89558 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-ToString.webp\" alt=\"Java ToString()\" width=\"400\" height=\"243\" \/><\/a><\/p>\n<h4>Parameters<\/h4>\n<p>NONE<\/p>\n<h4>Return Value<\/h4>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Type:<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Description<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">A string:<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The content of the string.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public static String toString()\r\npublic static String toString(int i)\r\npublic static String toString(int i, int base)<\/pre>\n<p>We have three variants of the Java String toString() method. All three variants return the String representation for an Integer. We will discuss all three variants in the latter part of this tutorial.<\/p>\n<h3>toString() with base 10 and base 2<\/h3>\n<p>In this programming example, we will see how the toString() Java method works. Here, we create an object of base 10 and try to get the String representation of that object in base 10 and base 2.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class TechVidvan_toString {\r\nPublic static void main(String[] args) {\r\n\/\/in base 10\r\nInteger obj = new Integer(10);      \r\n\/\/used toString() method for String equivalent of the Integer     \r\nString str1 = obj.toString();     \r\nString str2 = obj.toString(80);       \r\n\/\/in base 2\r\nString str3 = obj.toString(658,2);        \r\n\/\/ Printed the value of all the String variables\r\nSystem.out.println(str1);        \r\nSystem.out.println(str2);        \r\nSystem.out.println(str3);\r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n10<br \/>\n80<br \/>\n1010010010<\/p>\n<h3>toString() With Decimal<\/h3>\n<p>This example shows how the Java toString() method works with decimal or float variables.<\/p>\n<p>Here, we have created an object of base 10. Then, we have passed a decimal value (in the previous program, we passed an integer value 80, which returned 80 as an output).<\/p>\n<p>This will throw a compilation error with the message \u201c The system toString( int) in the type Integer isn&#8217;t applicable for the arguments( double).\u201d That&#8217;s why we have to use the Double class toString() system to get the String representation of pier\/ double, which we will bandy in the coming illustration.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class TechVidvan_toString {\r\nPublic static void main(String[] args) {\r\n\/\/in base 10\r\nInteger obj =newInteger(10);     \r\n\/*\r\n*  The system toString( int) in the type Integer is\r\n* not applicable for the arguments( pier or double)\r\n*\/\r\nString str1 = obj.toString(69.47);\r\n( str1);\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nUnresolved compilation problem<\/p>\n<h3>ToString() With Double<\/h3>\n<p>As an outgrowth of the former illustration, we will bandy getting the String representation of pier\/ double variables in this illustration.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class TechVidvan_toString {   \r\nPublic static void main(String[] args) [  \r\n\/\/ Initialized a double variable with the value146.39\r\nDouble dbl =146.39d;\r\n\/\/ Getting the String representation of the double variable\r\nString str = Double.toString(dbl);\r\nSystem.out.println(str);\r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n146.39<\/p>\n<h3>Scenarios<\/h3>\n<p><strong>Scenario 1:<\/strong> Illustrating Java toString(int num, int base value)<\/p>\n<p><strong>Explanation:<\/strong> Then, we will illustrate the Java toString( int number, int base value) and try to get the String representation of the different cases. In this script, we&#8217;ve created an object in base 10. We&#8217;ve also used Java toString( int num, int base value) to try the base values 2, 8, 16, and 10. Later, we&#8217;ve published the String representation of each of these base values for the specified integer value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class TechVidvan_toString {\r\nPublic static void main(String[] args) {\r\n\/\/ in base 10\r\nInteger obj = new Integer(10);\r\n\/\/ in base 2\r\nString str = obj.toString(9876,2);\r\n\/\/ It returns a string representation    \r\nSystem.out.println(\"String Value of 9876 in base 2 = \"+  str);\r\nSystem.out.println();    \r\n\/\/ in base 8\r\nstr = obj.toString(350,8);\r\n\/\/ It returns a string representation        \r\nSystem.out.println(\"String Value of 350 in base 8 = \"+ str);  \r\nSystem.out.println();      \r\n\/\/ in base 16 \r\nstr = obj.toString(470,16);\r\n\/\/ It returns a string representation\r\n System.out.println(\" String Value of 470 in base 16 = \" str);\r\nSystem.out.println();   \r\n\/\/ in base 10\r\nstr = obj.toString(451,10);     \r\n\/\/ It returns a string representation\r\nSystem.out.println(\" String Value of 451 in base 10 = \" str);\r\n}\r\n}<\/pre>\n<p><strong>Output<\/strong><br \/>\nString value of 9876 in base 2 = 10011010010100<br \/>\nString value of 350 in base 8 = 536<br \/>\nString value of 470 in base 16 = 1d6<br \/>\nString value of 451 in base 10 = 451<\/p>\n<p><strong>Script 2:<\/strong> We will try Java toString on the negative Integers in this script.<\/p>\n<p><strong>Explanation:<\/strong> Then, we used the same program( as in script 1). The only difference is the use of a negative number. We didn&#8217;t change the base value, but the Integer values have been changed into negative figures.<\/p>\n<p>As we examined this program, we learned that the Java toString() method works well with negative figures.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class TechVidvan_toString {\r\nPublic static void main(String[] args) {\r\n\/\/ in base 10\r\nInteger obj = new Integer(10);\r\n\/\/ in base 2\r\nString str = obj.toString(9876,2);\r\n\/\/ It returns a string representation   \r\nSystem.out.println(\" String Value of 9876 in base 2 = \" str); \r\n System.out.println();       \r\n\/\/ in base 8\r\nstr = obj.toString(350,8);\r\n\/\/ It returns a string representation\r\nSystem.out.println(\" String Value of 350 in base 8 = \" str);\r\nSystem.out.println();\r\n\/\/ in base 16\r\nstr = obj.toString(470,16);\r\n\/\/ It returns a string representation\r\nSystem.out.println(\" String Value of 470 in base 16 = \" str);\r\nSystem.out.println();\r\n\/\/ in base 10\r\nstr = obj.toString(451,10);\r\n\/\/ It returns a string representation \r\nSystem.out.println(\" String Value of 451 in base 10 = \" str);\r\n}\r\n}<\/pre>\n<p><strong>Output<\/strong><br \/>\n<strong>String value of 9876 in base 2 =<\/strong> -10011010010100<br \/>\n<strong>String value of 350 in base 8 =<\/strong> -536<br \/>\n<strong>String value of 470 in base 16 =<\/strong> -1d6<br \/>\n<strong>String value of 451 in base 10 =<\/strong> -451<\/p>\n<h3>Conclusion<\/h3>\n<p>In this tutorial, we thoroughly understood the Java toString() method. The programming exemplifications for each base value were applicable to learning about converting an Integer into a String representation for a particular base value.<\/p>\n<p>This tutorial was explained using different scenarios to help us better understand. We also learned about the negative and decimal\/floating-point number behavior when used in the toString() method. We explored the frequently asked questions, which helped us\u00a0understand this method clearly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The toString() method returns the String representation of the object. However, the Java compiler internally invokes the toString() method on the object if you publish any object. So, booting the toString() system returns the&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447371,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,5561,327,250,5562,243],"class_list":["post-89386","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-tostring-method","tag-java-tutorials","tag-learn-java","tag-tostring-method","tag-tostring-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java toString() Method - TechVidvan<\/title>\n<meta name=\"description\" content=\"In this Java toString Method, we learned about the negative and decimal\/floating-point number behaviour when used in the toString() method.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java toString() Method - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In this Java toString Method, we learned about the negative and decimal\/floating-point number behaviour when used in the toString() method.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-05T12:30:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-to-string-method.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=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java toString() Method - TechVidvan","description":"In this Java toString Method, we learned about the negative and decimal\/floating-point number behaviour when used in the toString() method.","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:\/\/techvidvan.com\/tutorials\/java-tostring-method\/","og_locale":"en_US","og_type":"article","og_title":"Java toString() Method - TechVidvan","og_description":"In this Java toString Method, we learned about the negative and decimal\/floating-point number behaviour when used in the toString() method.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-06-05T12:30:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-to-string-method.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java toString() Method","datePublished":"2025-06-05T12:30:35+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/"},"wordCount":612,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-to-string-method.webp","keywords":["java","java tostring() method","java tutorials","Learn Java","tostring() method","tostring() method In Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/","name":"Java toString() Method - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-to-string-method.webp","datePublished":"2025-06-05T12:30:35+00:00","description":"In this Java toString Method, we learned about the negative and decimal\/floating-point number behaviour when used in the toString() method.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-to-string-method.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-to-string-method.webp","width":1200,"height":628,"caption":"java to string() method"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-tostring-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java toString() Method"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89386"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89386\/revisions"}],"predecessor-version":[{"id":447836,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89386\/revisions\/447836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447371"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}