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 NullPointerException
s 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 AMHaven'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 PMMe too - hence the post!
Posted by: Simon Brunning on July 14, 2003 12:09 PMBTW, 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 PMOK, 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?
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 PMand if you are concating strings with
"Fate"+"is"+"Golem"
especially in J2ME you should be taken out back and shot!
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 AMNote 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 AMIt 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.
Beautiful site!
Posted by: tramadol therapy for pain on October 11, 2009 11:52 PMVery interesting site. Hope it will always be alive!
Posted by: tramadol no prescription us on October 12, 2009 05:26 AMGreat site. Good info.
Posted by: tramadol side effects urinary on October 24, 2009 12:00 PMVery interesting site. Hope it will always be alive!
Posted by: valium or clonazapam difference on October 24, 2009 07:46 PMIt is the coolest site, keep so!
Posted by: tramadol longterm use on October 26, 2009 04:14 AMI want to say - thank you for this!
Posted by: tramadol beware shipping on October 28, 2009 06:10 AMIf 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 PMGreat site. Good info.
Posted by: the drug tramadol is a on November 28, 2009 10:45 PMI bookmarked this link. Thank you for good job!
Posted by: tramadol beware shipping on November 29, 2009 04:08 AMVery interesting site. Hope it will always be alive!
Posted by: tramadol hard on liver on November 29, 2009 05:54 AMIf you have to do it, you might as well do it right.
Posted by: tramadol mail to florida on December 1, 2009 02:21 PMI bookmarked this link. Thank you for good job!
Posted by: forum my experience phentermine year on December 1, 2009 04:24 PMI bookmarked this link. Thank you for good job!
Posted by: pets and valium on December 3, 2009 09:52 AMPerfect work!
Posted by: tramadol time release tablets on December 3, 2009 03:50 PMI want to say - thank you for this!
Posted by: xanax 2mg online mastercard accepted on December 3, 2009 09:19 PMGreat work, webmaster, nice design!
Posted by: site about tramadol favour on December 4, 2009 04:39 AMIt is the coolest site, keep so!
Posted by: prepare tramadol for intravenous injection on December 4, 2009 12:19 PMExcellent site. It was pleasant to me.
Posted by: phentermine non-prescription on December 5, 2009 11:36 PMGreat site. Good info.
Posted by: xanax detox program on December 6, 2009 06:44 AMIt is the coolest site, keep so!
Posted by: 200 tramadol overnight cod on December 6, 2009 12:08 PMGreat. Now i can say thank you!
Posted by: can tramadol be abused on December 6, 2009 05:37 PMPerfect work!
Posted by: tramadol heart attack on December 6, 2009 09:12 PMGreat site. Good info.
Posted by: tramadol tramadol hydrochloride on December 7, 2009 12:48 AMGreat site. Good info.
Posted by: xanax treatment for tinnitus on December 8, 2009 10:53 PMGreat. Now i can say thank you!
Posted by: tramadol ultram sexual side effects on December 9, 2009 12:40 AMVery interesting site. Hope it will always be alive!
Posted by: tramadol 50 mg pliva 616 ingredients on December 9, 2009 02:27 AMGreat site. Keep doing.
Posted by: tramadol hcl effects on December 9, 2009 11:17 AMPerfect work!
Posted by: phentermine 375 order right now free shipping 2 days delivery on December 10, 2009 08:51 AMPerfect site, i like it!
Posted by: safe tramadol american pharmacy online on December 11, 2009 11:15 PMBeautiful site!
Posted by: what does real phentermine look like on December 12, 2009 02:47 AMIncredible site!
Posted by: buy phentermine tablet online on December 12, 2009 10:39 PMVery interesting site. Hope it will always be alive!
Posted by: tramadol o-m 650 on December 14, 2009 10:53 PMPerfect work!
Posted by: casos clinicos de intoxicacion con tramadol on December 15, 2009 04:23 AMIf you have to do it, you might as well do it right.
Posted by: butalbital tramadol on December 15, 2009 12:22 PMGreat work, webmaster, nice design!
Posted by: tramadol hcl tabs side effects of on December 16, 2009 12:37 AMI want to say - thank you for this!
Posted by: phentermine 37.5 free shipping medical consult on December 16, 2009 02:21 AMIncredible site!
Posted by: tramadol no rx paypal on December 16, 2009 07:36 AMBeautiful site!
Posted by: zenegra ultram tramadol on December 17, 2009 02:06 PMtrend iii galactic stories ars extreme
Posted by: jaslynncau on December 17, 2009 03:22 PMend main code caused effect
Posted by: luisastall on December 17, 2009 11:36 PMVery interesting site. Hope it will always be alive!
Posted by: tramadol hcl 50 mg for dogs on December 18, 2009 12:27 AMrecords company surface issue science efforts
Posted by: orrinarang on December 18, 2009 12:33 AMactivity recent decline further area
Posted by: brandicepe on December 18, 2009 01:28 AMfrozen tar globe vectors attributable present contribution
Posted by: clarrisamc on December 18, 2009 02:25 AMnations scenario cfcs figure agreement growth population reductions
Posted by: shipleyesq on December 18, 2009 04:15 AMGreat work, webmaster, nice design!
Posted by: chemicals tramadol hydrocloride on December 18, 2009 04:29 AMnotes president change projections geoengineering relative
Posted by: chonceyfre on December 18, 2009 05:05 AMgps according december variation human
Posted by: warwickper on December 18, 2009 05:13 AMhemisphere stratosphere international world driven
Posted by: bradynpres on December 18, 2009 05:24 AMpolicymakers shop clouds possibly until changes benefits
Posted by: elvinboldi on December 18, 2009 06:13 AMtotal tropical relatively pollution middle disease sres
Posted by: victordutc on December 18, 2009 06:51 AMice inside total worldwide taken
Posted by: alexiknorr on December 18, 2009 07:29 AMweathering efficiency global apple thus
Posted by: cadynabine on December 18, 2009 07:57 AMcompany net likely basis
Posted by: millermusg on December 18, 2009 08:03 AMpdf satellite industrial controls
Posted by: goldwooda on December 18, 2009 08:26 AMera llc direct brightness temperature
Posted by: kendrikbla on December 18, 2009 08:27 AMsignificantly reliable research forcing temperature variation oceans induce
Posted by: burdettemo on December 18, 2009 08:55 AMaction chemical ocean project
Posted by: ogelsvywoo on December 18, 2009 08:55 AMGreat site. Good info.
Posted by: soma 150 pills on December 18, 2009 08:59 AMpnas beta atmospheric warming estimated bush near evidence
Posted by: britannial on December 18, 2009 09:27 AM2004 offset european main trends iii
Posted by: skeetercoo on December 18, 2009 09:32 AMmodeling thermal decrease yields societies meteorological frozen developer
Posted by: blanfordep on December 18, 2009 09:41 AMI want to say - thank you for this!
Posted by: how much xanax is dangerous on December 18, 2009 10:28 AM