Java Array or Java Arrays - Arrays in Java
- An array is a group of like-typed variables that are referred to by a common name. Arrays in Java work differently than they do in C/C++.
- Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
- Array in java is index-based, the first element of the array is stored at the 0 index.
- Java Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
What is Java Array ?
An array is a collection of variables of the same type. Array stores a fixed size sequential collection of elements of the same type. An array is used to store a collection of data. Each item in an array is called an element, and each element is accessed by its numerical index. In a real-world programming situation, you would probably use one of the supported looping constructs (for, while, and do-while) to iterate through each element of the array, rather than write each line individually. You will learn about the various looping constructs (for, while, and do-while) in the java looping or iteration statements of Control Flow section.
About Java Arrays ?
- In Java all arrays are dynamically allocated.
- Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using sizeof.
- A Java array variable can also be declared like other variables with  after the data type.
- In Java all the arrays are indexed and declared by int only.
- The variables in the array are ordered and each have an index beginning from 0.
- Java array index ends at 2147483646.
What are the Advantages of Java Array ?
- Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
- Random access: We can get any data located at an index position.
- We can change the value of any indexed variable or value.
- We can store 2147483647 elements in Java array.
What are the Disadvantages of Java Array ?
- Size Limit: We can store only the fixed size of elements in the array.
- Java array size does not grow after the allocation.
- If you try to store long (big) elements in array, you will get performance problems.
Declaring Java Array Variable
Java Arrays are declared with  (square brackets). If you put  (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type. If you put  (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4. In this tutorial you can learn how to declare arrays, how to assign values to arrays and how get values from arrays.
// declares an array of integers
What is the Length of an Array in Java ?
In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.
In this tutorial you can learn Java Arrays with the following Java Primitive Data Types.