What is the max allowed size to create an array in Java?
class Array6
{
public static void main(String[] args)
{
int[] x=new int[2147483647];
System.out.println(x);
}
}
/*
Output:- Unexpected Output depending upon machine but not compilation error.
Explanation:- The max allowed Array size in Java is 2147483647 which is max value of int.
In the above case we may get OutOfMemoryError if sufficient heap memory is not
available. This is the problem with the machine but not with Java. If sufficient
memory is available then it will print reference value of the Array.
*/
{
public static void main(String[] args)
{
int[] x=new int[2147483647];
System.out.println(x);
}
}
/*
Output:- Unexpected Output depending upon machine but not compilation error.
Explanation:- The max allowed Array size in Java is 2147483647 which is max value of int.
In the above case we may get OutOfMemoryError if sufficient heap memory is not
available. This is the problem with the machine but not with Java. If sufficient
memory is available then it will print reference value of the Array.
*/
0 comments: