0% found this document useful (0 votes)
6 views2 pages

Arrays in Java

An array in Java is a data structure that holds multiple values of the same type, with indices starting from 0. Arrays must have a fixed size defined at creation, and accessing out-of-bounds indices results in an ArrayIndexOutOfBoundsException. They can be declared and initialized in various ways, including static initialization with predefined values.

Uploaded by

Lalitha yamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Arrays in Java

An array in Java is a data structure that holds multiple values of the same type, with indices starting from 0. Arrays must have a fixed size defined at creation, and accessing out-of-bounds indices results in an ArrayIndexOutOfBoundsException. They can be declared and initialized in various ways, including static initialization with predefined values.

Uploaded by

Lalitha yamini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Arrays in Java

1. Definition
An array in Java is a data structure that stores multiple values of the same type in a single
variable. Each element in the array is identified by an index, starting from 0.

2. Rules for Using Arrays


- All elements must be of the same data type (e.g., int, float, String).

- The size of the array is fixed and defined at the time of creation.

- Array indices start from 0 and go up to array.length - 1.

- Accessing an index outside the array bounds throws ArrayIndexOutOfBoundsException.

- Arrays can be single-dimensional or multi-dimensional.

3. Syntax
Declaration Syntax:

dataType[] arrayName; // preferred


OR
dataType arrayName[]; // valid but less preferred

4. Declaration and Initialization


1. Declare and then Initialize:

int[] numbers; // declaration


numbers = new int[5]; // initialization (creates space for 5 integers)

2. Declare and Initialize in One Line:

int[] numbers = new int[5]; // all values default to 0

3. Declare with Values (Static Initialization):


int[] numbers = {1, 2, 3, 4, 5};

You might also like