Results 1 to 10 of 10
  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default toString() vs valueOf()

    I have search online on the difference between toString() vs valueOf() and which is being preferred.

    What I have found is that some are claiming that to prefer to use toString() since valueOf() calls toString() which result in calling two methods.
    Other are claiming that valueOf() is better to use since we can ignore stating the data type and has more readability compare to toString().

    There are also some who claimed that using valueOf() is good as null reference need not be checked.

    so, in sort, which is preferable?

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: toString() vs valueOf()

    I suppose it depends on the situation and personal preference. And things like System.out.println(...) automatically handle this for you. Personally, I only use valueOf() if I'm working with primitives, otherwise I use toString(). Or, if I know I may encounter nulls and I just want them to display as "null" then I might use valueOf(), otherwise I'd check if the reference is null and handle it accordingly.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: toString() vs valueOf()

    Actually, String.valueOf() CALLS Object.toString() if the argument is an object.

    edit: But, in end effect I would tend, SLIGHTLY, to agree with those that say to always use valueOf, if you can afford to have NullPointerExceptions ignored, as a null object would produce the String "null", and it would also provide a uniform manner of doing things. However, as I said, I would NOT do it just to ignore "null checks" for objects (I would STILL perform those null checks in applicable instances), as that is likely to simply cover up problems that should be caught before producing the string "null" in the output, which might be overlooked for quite a long time and possibly hide other problems that could have been found sooner.
    Last edited by masijade; 09-20-2013 at 09:15 AM.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: toString() vs valueOf()

    Using valueOf() for that purpose is like thinking ostriches can bury their head in the sand: it works in a cartoon, but in real life you hit your head on the cold ground and break your nose.

    Not even for a templating engine would I find this useful, since you want null to print as empty, not as 'null'.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: toString() vs valueOf()

    Quote Originally Posted by gimbal2 View Post
    Using valueOf() for that purpose is like thinking ostriches can bury their head in the sand: it works in a cartoon, but in real life you hit your head on the cold ground and break your nose.

    Not even for a templating engine would I find this useful, since you want null to print as empty, not as 'null'.
    Actually, I WOULD prefer null objects produce "null" (although I would rather that null be caught beforehand), unless the other effect is NEEDED, as in JSF, and the like. IMHO producing an empty string for a null object is the wrong approach in most cases as it masks the null problem even more than producing "null" does. I would prefer an empty string produce an empty string and that my objects NOT BE null.
    Last edited by masijade; 09-20-2013 at 09:41 AM.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: toString() vs valueOf()

    Depends on what you template I guess. The result tends to be for the eyes of end users, and to them 'null' has absolutely nothing of value.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: toString() vs valueOf()

    Quote Originally Posted by gimbal2 View Post
    Depends on what you template I guess. The result tends to be for the eyes of end users, and to them 'null' has absolutely nothing of value.
    Yes, and in end effect, that is my point as well. Nulls should be caught. If you know it is possible for a variable to be null, WITHOUT it being the result of an error, then by all means show an empty string, otherwise, let it show null, and find the problem, even if that problem produces no other ill effects.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: toString() vs valueOf()

    I think we were already in total agreement from the first post, its just a vague topic to discuss ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    55
    Rep Power
    0

    Default Re: toString() vs valueOf()

    As a newbie I have found the vague discussion to be very helpful.

  10. #10
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2012, 11:10 AM
  2. How to display toString() inside another toString()
    By SVTermiCobra in forum New To Java
    Replies: 1
    Last Post: 08-08-2012, 01:49 AM
  3. toString
    By juanvalentinequintana in forum New To Java
    Replies: 2
    Last Post: 05-13-2011, 04:30 PM
  4. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 AM
  5. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •