0

My program needs to handle very big numbers as input, so I chose long. I have a error when I create an array of type long using a variable of type long as its size. Could someone please provide some insight about what went wrong here?

Error:

long[] Arr = new long[n];
                      ^    //incompatible types: possible lossy conversion from long to int

Code:

private static long foo(long n, long m) {
  if (n <= 1) return n;
  long[] Arr = new long[n];
  return 0;
}
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23
LED Fantom
  • 1,229
  • 1
  • 12
  • 32
  • 2
    I don't think you'd ever need 9,223,372,036,854,775,807 slots of data anytime soon. – Adel Khial Sep 20 '16 at 05:58
  • Possible duplicate of [http://stackoverflow.com/questions/14571557/create-an-array-of-long](http://stackoverflow.com/questions/14571557/create-an-array-of-long) – Rishal Sep 20 '16 at 06:17

2 Answers2

2

Looks like n is of type Long. But the length of an Array can only be an int

Jens
  • 67,715
  • 15
  • 98
  • 113
1

If you need to handle very big numbers in your program, consider using BigInteger or BigDecimal types. These types have no theoretical limit and allocate a much memory as needed. So it's limited only the amount of available memory.

mv200580
  • 702
  • 1
  • 6
  • 14