Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Initial value for BigInteger from another BigInteger

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use the BigInteger class in package java.math.BigInteger. A BigInteger is an arbitrary precision twos compliment binary number; it is for numbers that will not fit in a long. There are several constructors providing several ways to assign the initial value to a newly created BigInteger, and a static method, valueOf, that returns the BigInteger form of a specified long. There does not seem to be a direct way to get the initial value from another BigInteger. Does anyone know why? I can work around this omission by setting the value to zero, and adding the desired initial value. I suspect this is not a java design bug, but rather that I'm thinking wrong.
What should I do instead? BigInteger is not Cloneable. Thanks.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You missed one important feature of BigInteger -- namely that it's immutable. Therefore it isn't necessary to make copies of BigInteger objects. Just give your new variable a reference to an existing BigInteger object, that's all you need.
 
Charles Bradley
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, I missed the "immutable". Suppose x is an array of BigInteger. I want to compute x[i] = (x[j] + x[k]) *x[n] for various values of j, k, and n that are less than i and functions of i. The value of the reference at x[i] will change several times as I compute the value. The garbage collector will have a lot of work to do. Perhaps it will not be too much work. I won't worry until I feel some pain. Thanks for the quick help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!