DEV Community

velvizhi Muthu
velvizhi Muthu

Posted on

Module 4-Array

  1. What is an array? An array is a collection of similar data types.

Array Syntax : []
Declaration:
datatype[] arrayName; or datatype arrayName[];
Example:
int[] marks = {50,80,70};
boolean[] result ={true,false,true};
float[] s = {6.0f,5.0f,4.8f};

An array is index-based.
marks[0] = 50;
marks[1] = 80;
marks[2] = 70;

Program explanation:

int[] marks = {50,80,70};
length = 3
index starts from 0. --> length -1 (2)

Object:
Objects in Java ->Arrays are objects stored in heap memory.
arrayName = new datatype[size];
Example:
int[] marks = new int[3];

  1. Why do we use arrays in Java?
    We use arrays in Java because they make it easy to store and manage multiple values of the same type under a single variable name, instead of declaring many separate variables.
    example:
    int tamil = 50;
    int english = 80;
    int maths = 70;

  2. Key Points about Arrays:

Fixed size – Once created, the size of an array cannot be changed.

Same data type – All elements must be of the same type.

Index-based – Elements are accessed using an index, starting from 0 in most programming languages.

Homogeneous data stores only elements of the same data types.
First element-> index 0.
Second element-> index length - 1.
Print all elements in the array
Reverse the array.
Sum the elements in the array.
Store all values in a single array

Top comments (0)