Bear Bibeault wrote:Personally, I'd choose the latter when the value fits into a long. It just seems more "right", and because of the compile-time checking of the long constant.
In general I agree, but if I had a mix of some that fit and some that don't in close proximity, I might go with just using String for all of them for consistency, and count on my unit tests to catch any typos that make for illegal values.
Additionally, with BigDecimal it matters more to use the String c'tor because, e.g., "0.1" means exactly one-tenth, but 0.1 does not. The String c'tor to BD isn't subject to the floating point precision errors that the double c'tor is. If I were in the habit of using BD a fair amount in my code, I'd probably already have the habit of using its String c'tors, and I'd probably want to--and be naturally inclined to--stick with that habit for BI as well, more for the habit's sake than for specific BI benefits.
Randall Twede wrote:i noticed i can also do this
BigInteger n = BigInteger.valueOf(1234567L);
do you prefer one over the other if you have a choice?
Quite apart from the other good advice, my usual rule is to use a factory over a constructor if there's a choice, for the simple reason that a factory doesn't necessarily have to hand you a new object. This is true of all the wrapper class valueOf() methods, and also some instance methods like String.intern().
For example, it's quite possible that BigInteger.valueOf(1L) could return BigInteger.ONE, rather than a new BigInteger.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here