Previous
Next

Java Array or Java Arrays - Arrays in Java

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 ?

What are the Advantages of Java Array ?

What are the Disadvantages of Java Array ?

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
int[] anArray;

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.

Syntax :

< data type > < variable >[];
< data type >[] < variable >;

In this tutorial you can learn Java Arrays with the following Java Primitive Data Types.

Previous
Next