I'm a beginner in java and our professor avoided discussing BinInteger and BigDecimal classes from java.math package.I wonder why.Are they not that useful?When exactly we must need to use BigInteger?
6 Answers
You don't need them particularly often, but when you do need them you really need them. You really only need them when you need to actually store arbitrary precision integers or real numbers. long
goes up to 263-1, which is a pretty big number.

- 191,574
- 25
- 345
- 413
Always use the primitives when possible because:
- They have operators rather than methods, so code is easier to read/write.
- They are a lot more efficient.
long
, the largest primitive integer type, has a maximum value of 9,223,372,036,854,775,807
, or 2^63 - 1
, and a minimum value of -2^63
.
double
, the most precise primitive floating point type, has 64 bits of precision, which is a lot.
However, if you really need an arbitrarily large/small integer or arbitrarily precise decimal number, the BigInteger
and BigDecimal
types are appropriate. Such scenarios aren't that common, however, which is probably why your professor didn't discuss these types.
Decimal data types are essential when dealing with currencies

- 904
- 1
- 12
- 31
-
"`BigDecimal` data type is essential when dealing with currencies" – JockX Mar 17 '16 at 21:33
-
I was thinking more about multiple languages; if Mobasher is learning he should _hopefully_ be learning more than just Java! I was 'lucky' enough to learn Eiffel at college! – Neil Benn Mar 18 '16 at 00:33
The primitive types of int and long have a limited range of values they can represent. The same is true for the floating point primitives float and double. There however you also face the issue of a limited precision. For many cases this does not pose any problem however when larger numbers or exact precision is required (e.g. in a banking application you will want to be as precise as possible) you will use BigInteger
and BigDecimal
however.

- 7,238
- 9
- 41
- 64
BIGINT is always the product of two Ints. Example 99X99 = 10000 upto twice as big.

- 5,581
- 1
- 26
- 35
Both of the libraries you mentioned has their uses, if they didn't they wouldn't exist. However, your teacher probably elected not to discuss them as you probably wont be using them in your specific course. (He has to put a limit somewhere, you cannot cover the entirety of Java libs in one course.)

- 64
- 2