{"id":95,"date":"2013-07-03T02:23:40","date_gmt":"2013-07-03T02:23:40","guid":{"rendered":"https:\/\/code4reference.com\/?p=95"},"modified":"2023-10-01T13:33:46","modified_gmt":"2023-10-01T13:33:46","slug":"code4referencewhich-one-is-better-valueof-or-tostring-in-java-code4reference","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=95","title":{"rendered":"which one is better valueOf() or toString() in Java"},"content":{"rendered":"<p>Developers often get into dilemma when they have to convert the object to string representation by choosing between <code>valueOf()<\/code> and <code>toString()<\/code> method. Because both the methods do the same thing but in a bit different ways. To give you more context, <code>toString()<\/code> method is present in <code>Object<\/code> class and generally overridden in the derived class where as <code>valueOf()<\/code> is overloaded static methods and present in the String class. The overloaded method can handle primitive types and objects. This method is not standard method as <code>toString()<\/code>. Some of the classes e.g Integer, Float, Double, etc. also have static implementation. Let\u2019s look at the examples of these methods.<\/p>\n<pre>\n\/\/1st method\npublic String getString(int num, Boolean boolvar) {\n \/\/Do something with num and boolvar\n  return String.valueOf(num) + \" \" +  String.valueOf(boolvar);\n }\n\n\/\/2nd method\npublic String getString(int num, Boolean boolvar) {\n \/\/Do somenthing with num and boolvar\n return Integer.toString(num) + Boolean.toString();\n }\n<\/pre>\n<p>In case of <code>String.valueOf()<\/code> method you don\u2019t have to worry, the overloaded method handles primitive types as well as objects. This method provides an abstraction. If argument type changes you don\u2019t have to change the implementation but this is not the case with <code>toString()<\/code> method. <code>String.valueOf()<\/code> has more readability.<\/p>\n<p>If you have an array of Object class containing Float, Integer, Char etc. First you have to identify all the object then type-cast to appropriate class and call <code>toString()<\/code> method. Which is kind of inefficient. If <code>valueOf()<\/code> method is called then it matches against the most suitable overloaded method and then <code>toString()<\/code> is called. This code structure follows good design pattern and looks neat. Here is some implementation example of <code>valueOf()<\/code> method present in <code>String class<\/code>.<\/p>\n<pre>\npublic static String valueOf(Object obj) {\n   return (obj == null) ? \"null\" : obj.toString();\n}\npublic static String valueOf(int i) {\n   return Integer.toString(i);\n}\n<\/pre>\n<p>Let\u2019s take another example of these two methods.<\/p>\n<pre>\nDouble d = null ;\nSystem.out.println(String.valueOf(d)); \/\/Prints \"null\"\nSystem.out.println(Double.toString(d)); \/\/Throws NullPointerException \n<\/pre>\n<p><code>String.valueOf()<\/code> prints \u201cnull\u201d if the reference is null whereas <code>Dobule.toString()<\/code> throws <code>NullPointerException<\/code>. Ideally references should be checked for <code>null<\/code> before calling <code>toString()<\/code>. This check can be avoided if you use <code>String.valueOf()<\/code> method.<br \/>\nPlease share your thoughts as comment below.<\/p>\n<p>Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below. You can find <a href=\"http:\/\/www.facebook.com\/Code4Reference\">Facebook page here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developers often get into dilemma when they have to convert the object to string representation by choosing between valueOf() and toString() method. Because both the methods do the same thing but in a bit different ways. To give you more context, toString() method is present in Object class and generally overridden in the derived class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":1,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":346,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions\/346"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}