Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does BigInteger ever overflows?

The API docs says

All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.

Does this implies BigInteger will never overflow assuming you have sufficient memory available ? If so why do we let some "type" overflow and some don't ?

As the language evolves, will it favor the types which hide overflowing mechanism from the programmers ?

like image 572
Ravi Gupta Avatar asked Jan 29 '10 16:01

Ravi Gupta


1 Answers

BigInteger will never overflow assuming you have enough memory to handle it.

To answer your question of why we let some types overflow and not others:

BigInteger is not really a type. It's a class. It's a wrapper class designed to give you the same functionality as an int, but allows you to use numbers as big as you need without the worry of overflow.

Types do overflow because they are simply a couple of bytes (exact amount depends on the type) of memory, and once that small amount of memory overflows, so do the numbers.

No class "overflows" unless it is specifically designed to do so (or if you run out of resources). A class is defined with enough memory for everything it contains, which would mostly be references to other classes or other data structures.

like image 114
Aaron Avatar answered Sep 23 '22 01:09

Aaron