I'm sure many of you think this is a dumb question, but after searching the forums and reading the javadocs I am still unsure of the difference between the two. I am retrieving info from a database, and I am using the ResultSetMetaData to get the type of a field. The type is returning a BigDecimal. All the methods I need to use accept a double as the parameter.
So my question is:
1) In laymen terms, what exactly is the difference between Double and BigDecimal?
2) Is there any harm in using the doubleValue of this BigDecimal in all of my methods?
Thanks, since I don't need that much precision I believe Double will be just fine for me :)
I have a follow up question I hope you can answer:
1) I am confused about which primitives the java.sql.Types match up too. Some are obvious, like Types.INTEGER. But others are not so obvious, I listed these below. I put down what I think some of them mean, but I could be way off. Can you help me with this?
All drivers that I have use support getObject(). I think there is some kind of translation table built into the driver itself which gives you the approprate translation. I've seen somewhere where some driver ddidn't support some of the more esoteric field types.
Make sure you know the difference between double (a
primitive type) and Double (a class type).
Yeah, I do. BigInteger has a doubleValue() method to get the double primitive which my methods use. I just figured I would ask the difference between BigDecimal and Double (instead of double) to avoid all of those "Stupid newbie, one's a class and the other is a primitive" smart @$$ answers. Thanks!
Double holds about 17 digits (I think) of precision.
BigDecimal can hold many, many, many digits of
precision at the expense of memory and speed..
BigDecimal can make your program run slower and use
more memory, but if you need the precision, WTF.
Another big difference between double and BigDecimal is that doubles stores it's fraction part in binary and BigDecimal stores it's fraction part in decimal. This matters becasue there is no way to exactly represent 0.1 in binary, for example. A truncated repeating bincimal (my word) that approximates the value of 0.1 is used instead.
Note that in order to make use of BigDecimal's ability to represent these numbers properly, it is neccesary to initialize the BigDecimal with a string or a type other than double or float. If you use a double to initialize the BigDecimal, the approximation will be passed on to the BigDecimal instance.