Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 BigInteger values?
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
randint




PostPosted: Thu Dec 29, 2011 6:03 pm   Post subject: BigInteger values?

OK, so theere is this thing called BigInteger whose type is immutable reference, does that mean that it can hold an integer of unlimited length, at least in theory, or only the RAM can limit its size (before a java.lang.OutOfMemoryError is thrown into the Java Virtual Machine)? Oh, and there is this thing called BigDecimal as well
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Dec 29, 2011 6:24 pm   Post subject: RE:BigInteger values?

The size of an integer stored by BigInteger (or a floating-point value stored by BigDecimal) is determined exclusively by the memory available to the JVM. Obviously, if the JVM has already used nearly all of its memory, it will not be able to allocate large arrays needed to store large numbers. BigInteger and BigDecimal both use very efficient implementations (it might be possible to be more efficient if you are aiming for an obscure corner case, but probably not in general).

The above has nothing to do with "immutable reference".

The "immutable reference" phrase means the following:
- behaves largely like String does (String is also an immutable reference type)
- cannot change its contents
- any mutator (change) methods create new instances with the new value stored in them

This doesn't work:
Java:

BigInteger foo = new BigInteger ("1234");
foo.add(1);  // no effect; silently creates new BigInteger with value 1235 and immediately discards it
// foo still represents the number 1234


This works:
Java:

BigInteger foo = new BigInteger ("1234");
foo = foo.add(foo);
// now foo represents the number 2468
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: