I use this code in my class. but its shows the error. why we cant assign long value in the String array declaration? It showing possible loss converstion.
long n=10000000;
String ar[]=new String[n];
I use this code in my class. but its shows the error. why we cant assign long value in the String array declaration? It showing possible loss converstion.
long n=10000000;
String ar[]=new String[n];
This is correct declaration. Use int
instead of long
. The Java JVM does not allow creating array in the size, range of long
data type, so it is producing the warning (error) at the compile time.
int n=10000000;
String ar[]=new String[n];
I tried the following code:
public class Array {
public static void main(String args[]) {
int a[] = new int[Integer.MAX_VALUE];
}
}
Got the following exception: Requested array size exceeds VM limit
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at gen2dArray.main(Array.java:7)
This question is likely the same as this
It is related to memory allocation. But we can use multi dimensional array to handle this.
On maximum integer array of string will take a huge amount of Memory.
Try use HashMap if you need to use large of array.