wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Initial value for BigInteger from another BigInteger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initial value for BigInteger from another BigInteger" Watch "Initial value for BigInteger from another BigInteger" New topic
Author

Initial value for BigInteger from another BigInteger

Charles Bradley
Greenhorn

Joined: Oct 04, 2009
Posts: 25
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Greenhorn

Joined: Oct 04, 2009
Posts: 25
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Initial value for BigInteger from another BigInteger
 
Similar Threads
BigInteger
Big Integers
BigInteger Power/Exponent BigInteger
Assignment OOP-2 - converting an object to an Integer
Convert Hex To Decimal