July 14, 2003
toString - just say no!

Java tip of the day - a must for newbies (are you reading this, Steve?), and a valuable reminder to the experienced: Kasia's The abuse and over-use of toString().

I was working on other people's code last week, and I wish they had read this! The debug code was using toString() all over the place, and NullPointerExceptions were frequent.

To sum up, the rules with toString() are: always override it, and never call it (directly). Instead, use String.valueOf() at all times.

Via Erik.

Posted to Java by Simon Brunning at July 14, 2003 10:55 AM
Comments

Haven't got this far in the book yet but it makes sense to me which must be a good thing. Now all I have to do is remember it.

Posted by: Stevan Rose on July 14, 2003 12:06 PM

Me too - hence the post!

Posted by: Simon Brunning on July 14, 2003 12:09 PM

BTW, Kasia points out that String.valueOf() is used *implicitly* whenever objects are coerced to strings. But given that the whole idea here is to bulletproof code, I'd prefer *not* to rely on this implicit behaviour, and to call String.valueOf() explicitly. YMMV.

Posted by: Simon Brunning on July 14, 2003 12:16 PM

OK, Simon. Following your logic, I shouldn't depend on new to allocate memory for me, I should call the OS directly to allocate memory to make sure my code is bulletproof.

Is that right?

Posted by: no one on July 14, 2003 03:55 PM

I *don't* like implicit coersions. A metter of taste, perhaps. Or are you talking about something other than calling String.valueOf() explicitly?

Posted by: Simon Brunning on July 14, 2003 04:43 PM

and if you are concating strings with

"Fate"+"is"+"Golem"


especially in J2ME you should be taken out back and shot!

Posted by: Fred Grott on July 14, 2003 10:13 PM

Fred - Isn't that a myth now?

I was sure most of the compilers magiked that away by now.

Posted by: Daniel Sheppard on July 15, 2003 02:30 AM

Note that I'm only advocating not using toString() - not avoiding non-static methods altogether! Nor am I suggesting that you should avoid explicit checks for null in general.

toString() is a special case, as far as I'm concerned, 'cos it's almost exclusivly used in exception handling and logging code, which should not fail under *any* curcumstances. Yes, if you have a null where you shouldn't, something has gone wrong and your program is almost certainly going to fail Real Soon Now. But you *need* your logging and exception handling code to work to help you work out what is going on...

See http://www.freeroller.net/page/ceperez/20030714#non_static_method_calls_considered and its comments for more on this.

Posted by: Simon Brunning on July 15, 2003 10:44 AM

It seems really naive to blame the *caller* of toString() when it's the stupid author of the underlying class who did the wrong thing. If you can't trust toString() to work correctly, 100% of the time, you're better off choosing someone else's implementation of whatever functionality you are trying to use.

Posted by: Craig McClanahan on August 13, 2004 05:54 AM

Beautiful site!

Posted by: tramadol therapy for pain on October 11, 2009 11:52 PM

Very interesting site. Hope it will always be alive!

Posted by: tramadol no prescription us on October 12, 2009 05:26 AM

Great site. Good info.

Posted by: tramadol side effects urinary on October 24, 2009 12:00 PM

Very interesting site. Hope it will always be alive!

Posted by: valium or clonazapam difference on October 24, 2009 07:46 PM

It is the coolest site, keep so!

Posted by: tramadol longterm use on October 26, 2009 04:14 AM

I want to say - thank you for this!

Posted by: tramadol beware shipping on October 28, 2009 06:10 AM

If you have to do it, you might as well do it right.

Posted by: tramadol online pharmacy apo address on November 28, 2009 08:55 PM

Great site. Good info.

Posted by: the drug tramadol is a on November 28, 2009 10:45 PM

I bookmarked this link. Thank you for good job!

Posted by: tramadol beware shipping on November 29, 2009 04:08 AM

Very interesting site. Hope it will always be alive!

Posted by: tramadol hard on liver on November 29, 2009 05:54 AM

If you have to do it, you might as well do it right.

Posted by: tramadol mail to florida on December 1, 2009 02:21 PM

I bookmarked this link. Thank you for good job!

Posted by: forum my experience phentermine year on December 1, 2009 04:24 PM

I bookmarked this link. Thank you for good job!

Posted by: pets and valium on December 3, 2009 09:52 AM

Perfect work!

Posted by: tramadol time release tablets on December 3, 2009 03:50 PM

I want to say - thank you for this!

Posted by: xanax 2mg online mastercard accepted on December 3, 2009 09:19 PM

Great work, webmaster, nice design!

Posted by: site about tramadol favour on December 4, 2009 04:39 AM

It is the coolest site, keep so!

Posted by: prepare tramadol for intravenous injection on December 4, 2009 12:19 PM

Excellent site. It was pleasant to me.

Posted by: phentermine non-prescription on December 5, 2009 11:36 PM

Great site. Good info.

Posted by: xanax detox program on December 6, 2009 06:44 AM

It is the coolest site, keep so!

Posted by: 200 tramadol overnight cod on December 6, 2009 12:08 PM

Great. Now i can say thank you!

Posted by: can tramadol be abused on December 6, 2009 05:37 PM

Perfect work!

Posted by: tramadol heart attack on December 6, 2009 09:12 PM

Great site. Good info.

Posted by: tramadol tramadol hydrochloride on December 7, 2009 12:48 AM

Great site. Good info.

Posted by: xanax treatment for tinnitus on December 8, 2009 10:53 PM

Great. Now i can say thank you!

Posted by: tramadol ultram sexual side effects on December 9, 2009 12:40 AM

Very interesting site. Hope it will always be alive!

Posted by: tramadol 50 mg pliva 616 ingredients on December 9, 2009 02:27 AM

Great site. Keep doing.

Posted by: tramadol hcl effects on December 9, 2009 11:17 AM

Perfect work!

Posted by: phentermine 375 order right now free shipping 2 days delivery on December 10, 2009 08:51 AM

Perfect site, i like it!

Posted by: safe tramadol american pharmacy online on December 11, 2009 11:15 PM

Beautiful site!

Posted by: what does real phentermine look like on December 12, 2009 02:47 AM

Incredible site!

Posted by: buy phentermine tablet online on December 12, 2009 10:39 PM

Very interesting site. Hope it will always be alive!

Posted by: tramadol o-m 650 on December 14, 2009 10:53 PM

Perfect work!

Posted by: casos clinicos de intoxicacion con tramadol on December 15, 2009 04:23 AM

If you have to do it, you might as well do it right.

Posted by: butalbital tramadol on December 15, 2009 12:22 PM

Great work, webmaster, nice design!

Posted by: tramadol hcl tabs side effects of on December 16, 2009 12:37 AM

I want to say - thank you for this!

Posted by: phentermine 37.5 free shipping medical consult on December 16, 2009 02:21 AM

Incredible site!

Posted by: tramadol no rx paypal on December 16, 2009 07:36 AM

Beautiful site!

Posted by: zenegra ultram tramadol on December 17, 2009 02:06 PM

trend iii galactic stories ars extreme

Posted by: jaslynncau on December 17, 2009 03:22 PM

end main code caused effect

Posted by: luisastall on December 17, 2009 11:36 PM

Very interesting site. Hope it will always be alive!

Posted by: tramadol hcl 50 mg for dogs on December 18, 2009 12:27 AM

records company surface issue science efforts

Posted by: orrinarang on December 18, 2009 12:33 AM

activity recent decline further area

Posted by: brandicepe on December 18, 2009 01:28 AM

frozen tar globe vectors attributable present contribution

Posted by: clarrisamc on December 18, 2009 02:25 AM

nations scenario cfcs figure agreement growth population reductions

Posted by: shipleyesq on December 18, 2009 04:15 AM

Great work, webmaster, nice design!

Posted by: chemicals tramadol hydrocloride on December 18, 2009 04:29 AM

notes president change projections geoengineering relative

Posted by: chonceyfre on December 18, 2009 05:05 AM

gps according december variation human

Posted by: warwickper on December 18, 2009 05:13 AM

hemisphere stratosphere international world driven

Posted by: bradynpres on December 18, 2009 05:24 AM

policymakers shop clouds possibly until changes benefits

Posted by: elvinboldi on December 18, 2009 06:13 AM

total tropical relatively pollution middle disease sres

Posted by: victordutc on December 18, 2009 06:51 AM

ice inside total worldwide taken

Posted by: alexiknorr on December 18, 2009 07:29 AM

weathering efficiency global apple thus

Posted by: cadynabine on December 18, 2009 07:57 AM

company net likely basis

Posted by: millermusg on December 18, 2009 08:03 AM

pdf satellite industrial controls

Posted by: goldwooda on December 18, 2009 08:26 AM

era llc direct brightness temperature

Posted by: kendrikbla on December 18, 2009 08:27 AM

significantly reliable research forcing temperature variation oceans induce

Posted by: burdettemo on December 18, 2009 08:55 AM

action chemical ocean project

Posted by: ogelsvywoo on December 18, 2009 08:55 AM

Great site. Good info.

Posted by: soma 150 pills on December 18, 2009 08:59 AM

pnas beta atmospheric warming estimated bush near evidence

Posted by: britannial on December 18, 2009 09:27 AM

2004 offset european main trends iii

Posted by: skeetercoo on December 18, 2009 09:32 AM

modeling thermal decrease yields societies meteorological frozen developer

Posted by: blanfordep on December 18, 2009 09:41 AM

I want to say - thank you for this!

Posted by: how much xanax is dangerous on December 18, 2009 10:28 AM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?