Skip to main content

Command Palette

Search for a command to run...

Arrays in java

Published
1 min readView as Markdown

Introduction to Arrays in Java

In Java, an array is a collection of variables of the same type stored at contiguous memory locations. It is a data structure that allows you to store multiple values in a single variable, making it easier to manage and access data efficiently.

An array is defined by specifying its type and the number of elements it can hold. Each element in the array can be accessed using its index, with the first element having an index of 0 and the last element having an index of length-1.

Key Points:

  • Arrays are zero-indexed: The first element is at index 0.

  • The size of an array is fixed once it is created.

  • Arrays can store primitives (like int, char, boolean, etc.) or objects.

  • The array length can be found using the .length attribute.

Syntax for Declaring an Array:

int[] arrayName = new int[10];  // Declares an array of integers with 10 elements

Syntax for Initializing an Array:

int[] arrayName = {1, 2, 3, 4, 5};  // Declares and initializes an array in one step

Accessing Array Elements:

int element = arrayName[0];  // Accesses the first element of the array

More from this blog

Amit singh's blog

235 posts