-1
/    private static final 
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    long arraySize=10_000_000L;
    // TODO code application logic here
    Long[] largeArray = new Long[10000000];// compiles OK
    Long[] myIntArray = new Long[arraySize];// compile error
}

Can some one help me understand why I am getting this compiler error. "error: incompatible types: possible lossy conversion from long to int"

Leon Adler
  • 2,993
  • 1
  • 29
  • 42
ppa
  • 37
  • 1

2 Answers2

0

You cannot initialize an array with a long size. You can only implement an array using an integer size n that satisfies:

0 <= n <= Integer.MAX_VALUE

See Java SE specification > Array Access.

All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (ยง5.6.1) and become int values.

An attempt to access an array component with a long index value results in a compile-time error.

Zack
  • 3,819
  • 3
  • 27
  • 48
  • 1
    While you're right, there's nothing at the link you provide that backs up or validates your claim. โ€“ Makoto Jan 01 '16 at 07:08
  • Correct Makoto, I have revised my answer to include the Java SE spec. It specifically calls out compile-time exception for using long as length. โ€“ Zack Jan 01 '16 at 15:55
0

"Arrays must be indexed by int values... An attempt to access an array component with a long index value results in a compile-time error." --- Wikipedia https://en.wikipedia.org/wiki/Criticism_of_Java#Large_arrays

Ashish Ani
  • 324
  • 1
  • 7