Arrays in Java
Introduction
• In Java, array is an object that stores a fixed number of elements of
the same data type.
• It stores multiple elements under a single variable name.
• Syntax :
• int[] marks = {88, 74, 91, 82, 68, 94};
Why we need arrays ?
• Suppose we have 6 students and we want to store their marks:
• int marks1 = 88;
• int marks2 = 74;
• int marks3 = 91;
• int marks4 = 82;
• int marks5 = 68;
• int marks6 = 94;
• This works fine for a few students, but imagine we have 100 or 1000
students. Creating so many variables would be difficult and messy.
• Solution: Arrays allow us to store all student's marks in a single variable:
• int[] marks = {88, 74, 91, 82, 68, 94};
• Now, we can access any student's mark using an index, loop through the
marks, and perform calculations easily.