aspose file tools*
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
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: 18564
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initial value for BigInteger from another BigInteger