Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

Is there a limit to the number of elements a Java array can contain? If so, what is it?

1 Answer

0 votes
by (46k points)

In a recent HotSpot VM, the correct response is Integer.MAX_VALUE - 5. Once you go beyond that:

public class Foo {

  public static void main(String[] args) {

    Object[] array = new Object[Integer.MAX_VALUE - 4];

  }

}

You get:

Exception in thread "main" java.lang.OutOfMem

Related questions

0 votes
1 answer
asked Sep 19, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...