• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

BigInteger: which would you choose?

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so far i have been doing this
BigInteger n = new BigInteger("1234567");
i noticed i can also do this
BigInteger n = BigInteger.valueOf(1234567L);
do you prefer one over the other if you have a choice?
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree, it seems more "right" to me as well
 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:In general I agree, but ...



I agree. Good points. I'd go with consistency, and I'd never use floating point over strings for BigDecimal for accuracy reasons.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree when you create a BigDecimal out of a long literal value.

When using it with double, this is a tricky pitfall. Jeff already hinted at it. Try this:

Output: 0.1000000000000000055511151231257827021181583404541015625

I remember that this was a subject in one of the Java Puzzlers.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Winston
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i looked at some other classes where i used BigInteger and found this:

the comments are the code i found. the first line of the for loop replaces them. i think we can all agree tha in this case BigInteger.valueOf() is better. it is shorter and more understandable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic