• Post Reply Bookmark Topic Watch Topic
  • New Topic

BigInteger  RSS feed

 
Robert Richardson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know from the API that the BigInteger class has two fields ZERO and ONE. It seems like I should be able to initialize a new BigInteger using them. Something like:
BigInteger myBigInt = new BigInteger(//something here.ONE);
All I've been able to do is:
BigInteger myBigInt = new BigInteger("0");
myBigInt = myBigInt.ONE;
Which seems like a waste.

I guess I don't really get what the fields are for.

I could use a few words of wisdom on this.
Thanks
Robert
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to assign a BigInteger of value 1 to myBigInt, then this is the way to go:
 
Robert Richardson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works like a charm.
If myBigInt is a new instance of BigInteger how is it that I don't have to use "new" to create it?
Thanks
Robert
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Richardson:
Works like a charm.
If myBigInt is a new instance of BigInteger how is it that I don't have to use "new" to create it?
Thanks
Robert


myBigInt is a reference to a BigInteger -- it is not the instance. It may refer to a new instance, via the "new" operator. Or it can refer to an already generated instance -- in this case, one that represents a one.

Henry
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is correct; if you read the BigInteger API Documentation you find it has ONE ZERO and TEN as objects already set up.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note, this is NOT initializing a new instance to 1.

You are setting your reference to a BigInteger instance to the CONSTANT instance of BigInteger that represents 1.

Since BigInteger is a non-mutable class, you can't change the value of ANY BigInteger instance, so you'll treat it the same. If it was a mutable class (you could change the instance rather than create a new instance when you perform arithmetic) this might cause problems.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!