Java Array

Last Updated: March 18, 2022

Java Arrays are used to hold multiple values on a single variable.

In other words, an array is a collection of similar types of data.

If you want to store the names of 100 students you can declare an array like this.

String[] students = new String[100];
// or you create array  
String[] students;
students = new String[10];

declare array

initialize array

int numbers[] = {1, 4, 2, 0, 2, 1, 4, 3, 0, 2};
String days[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

access array

Limitation of the Java arrays

  • Size of the array has to be predetermined when you create the array
  • You can not increse the size dynamically
  • If arrays of a larger size than is required are declared, the memory may be wasted.
Test