Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, an array is not only a data structure, but also a data type. An array data type is a special kind of data type that represents a collection of elements of the same type, arranged in a fixed-size sequential order.
When you declare an array variable in Java, you must specify both the data type of the elements in the array and the size of the array. Here's an example:
int[] numbers = new int[5];
In this example, we declare an array variable called numbers
of the data type int[]
, which means "an array of integers". We also initialize the array to have a size of 5, which means it can hold 5 integers.
Once you have declared an array variable in Java, you can use it like any other variable of that data type. You can access individual elements of the array using their index, like this:
int firstNumber = numbers[0]; int secondNumber = numbers[1];
In this example, we access the first and second elements of the numbers
array using their indices, which are 0 and 1, respectively.
You can also modify the elements of an array by assigning new values to them, like this:
numbers[0] = 1; numbers[1] = 2;
In this example, we assign the values 1 and 2 to the first and second elements of the numbers
array, respectively.
Arrays in Java are a powerful and flexible data type that allow you to store and manipulate collections of data in a convenient and efficient way.