BigDecimal

Posted by Mike Haller on Friday, March 16. 2007 at 23:31
I've done a lot of String Concatenations to convert ints to Strings, so I can create BigDecimals. After some time, i learnt that this is ugly. Code like this can be found everywhere (not only in my code)

private static String EMPTY_STRING = "";
int invoiceNumber = 123;
new BigDecimal(EMPTY_STRING + invoiceNumber);


Why don't we use the much simpler form?

BigDecimal.valueOf(invoiceNumber);


Isn't this much easier to read andunderstand? Why the heck do we do such horrible things in the first place? A customer asked me and my collegue why we often use the term ""+int to convert something into a String and I couldn't give him an answer. I was so shocked as it turned out to be true and that I didn't know why I did that stuff over and over again.

Bad habits last long.


Kai Grabfelder
As beeing the collegue also using ""+int I have to second you. But I will try to use valueOf in the future ;-)

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications
 
Submitted comments will be subject to moderation before being displayed.
 

About

My name is Mike Haller and I'm a software developer and architect at Bosch Software Innovations in Germany. I love programming, playing games and reading books. I like good food, making photos and learning and mentoring about the craftsmanship of commercial software development. Stack Overflow profile for mhaller

Quicksearch