{"id":1435,"date":"2022-03-04T05:39:13","date_gmt":"2022-03-04T05:39:13","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1435"},"modified":"2022-03-04T05:39:13","modified_gmt":"2022-03-04T05:39:13","slug":"print-object-of-a-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/04\/print-object-of-a-class\/","title":{"rendered":"Print object of a class"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Java program to print the object<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {\n\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the Test class\n    Test obj = new Test();\n\n    \/\/ print the object\n    System.out.println(obj);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Test@512ddf17<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an object of the class&nbsp;<var>Test<\/var>. When we print the object, we can see that the output looks different.<\/p>\n\n\n\n<p>This is because while printing the object, the&nbsp;<code>toString()<\/code>&nbsp;method of the object class is called. It formats the object in the default format. That is,<\/p>\n\n\n\n<ul><li><strong>Test<\/strong>&nbsp;&#8211; name of the class<\/li><li><strong>@<\/strong>&nbsp;&#8211; joins the string<\/li><li><strong>512ddf17<\/strong>&nbsp;&#8211; hashcode value of the object<\/li><\/ul>\n\n\n\n<p>If we want to format the output in our own way, we need to override the&nbsp;<code>toString()<\/code>&nbsp;method inside the class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {\n\n  @Override\n  public String toString() {\n    return \"object\";\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create an object of the Test class\n    Test obj = new Test();\n\n    \/\/ print the object\n    System.out.println(obj);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>object<\/code><\/pre>\n\n\n\n<p>In the above example, the output has changed. It is because here we override the&nbsp;<code>toString()<\/code>&nbsp;method to return the string&nbsp;<var>object<\/var>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java program to print the object Output In the above example, we have created an object of the class&nbsp;Test. When we print the object, we can see that the output looks different. This is because while printing the object, the&nbsp;toString()&nbsp;method of the object class is called. It formats the object in the default format. That [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[573],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1435"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1435"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1435\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}