codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

18 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Arrays
☕Java Programming

Java Arrays

Updated 2026-05-12
30 min read

Java Arrays

Arrays are fundamental data structures in Java that allow you to store multiple values of the same type in a single variable. They are essential for managing collections of data efficiently and are used extensively in various programming scenarios, from simple lists to complex matrices.

In this tutorial, we will explore how to declare, access, modify elements, determine array lengths, loop through arrays, and even work with multidimensional arrays. Understanding these concepts is crucial as they form the building blocks for more advanced Java programming topics.

Declaring Arrays

An array in Java is a collection of elements of the same type stored at contiguous memory locations. You can declare an array by specifying the data type followed by square brackets [] and then the variable name.

Syntax

Java
1

or

Java
1dataType arrayName[];

Both syntaxes are valid in Java, but the first one (dataType[] arrayName;) is more commonly used.

Example: Declaring an Array of Integers

ArrayDeclaration.java
1public class ArrayDeclaration {
2 public static void main(String[] args) {
3 int[] numbers;
4 }
5}

Initializing Arrays

You can initialize an array at the time of declaration by providing a list of values enclosed in curly braces {}.

Syntax

Java
1dataType[] arrayName = {value1, value2, value3, ..., valueN};

Example: Initializing an Array of Strings

ArrayInitialization.java
1public class ArrayInitialization {
2 public static void main(String[] args) {
3 String[] fruits = {"Apple", "Banana", "Cherry"};
4 }
5}

Accessing and Changing Elements

You can access elements of an array using their index, which starts at 0. You can also modify the value of an element by assigning a new value to it.

Syntax

Java
1arrayName[index] = newValue;

Example: Accessing and Modifying Array Elements

ArrayAccess.java
1public class ArrayAccess {
2 public static void main(String[] args) {
3 int[] numbers = {10, 20, 30};
4
5 // Accessing elements
6 System.out.println(numbers[0]); // Output: 10
7 System.out.println(numbers[1]); // Output: 20
8
9 // Modifying elements
10 numbers[1] = 25;
11 System.out.println(numbers[1]); // Output: 25
12 }
13}

Array Length

The length of an array is the number of elements it can hold. You can access this using the length property.

Syntax

Java
1arrayName.length

Example: Using Array Length

ArrayLength.java
1public class ArrayLength {
2 public static void main(String[] args) {
3 int[] numbers = {1, 2, 3, 4, 5};
4
5 System.out.println("Number of elements: " + numbers.length); // Output: Number of elements: 5
6 }
7}

Looping Through Arrays

You can loop through arrays using various control structures like for, enhanced for, or while loops. The enhanced for loop is particularly convenient for iterating over arrays.

Example: Using a For Loop to Iterate Over an Array

ArrayLoop.java
1public class ArrayLoop {
2 public static void main(String[] args) {
3 int[] numbers = {1, 2, 3, 4, 5};
4
5 for (int i = 0; i < numbers.length; i++) {
6 System.out.println(numbers[i]);
7 }
8 }
9}

Output

Output
1
2
3
4
5

Example: Using an Enhanced For Loop to Iterate Over an Array

ArrayEnhancedLoop.java
1public class ArrayEnhancedLoop {
2 public static void main(String[] args) {
3 int[] numbers = {1, 2, 3, 4, 5};
4
5 for (int number : numbers) {
6 System.out.println(number);
7 }
8 }
9}

Output

Output
1
2
3
4
5

Multidimensional Arrays

Multidimensional arrays are arrays of arrays. They allow you to store data in a tabular form, similar to matrices.

Syntax

Java
1dataType[][] arrayName;

Example: Declaring and Initializing a 2D Array

MultiDimensionalArray.java
1public class MultiDimensionalArray {
2 public static void main(String[] args) {
3 int[][] matrix = {
4 {1, 2, 3},
5 {4, 5, 6},
6 {7, 8, 9}
7 };
8
9 // Accessing elements
10 System.out.println(matrix[0][0]); // Output: 1
11 System.out.println(matrix[1][1]); // Output: 5
12
13 // Modifying elements
14 matrix[2][2] = 10;
15 System.out.println(matrix[2][2]); // Output: 10
16 }
17}

Practical Example

Let's create a practical example that demonstrates the use of arrays, including declaring, initializing, accessing, modifying, and looping through elements.

Example: Student Grades Management

StudentGrades.java
1public class StudentGrades {
2 public static void main(String[] args) {
3 String[] students = {"Alice", "Bob", "Charlie"};
4 double[] grades = {85.5, 92.0, 78.5};
5
6 System.out.println("Student Grades:");
7 for (int i = 0; i < students.length; i++) {
8 System.out.println(students[i] + ": " + grades[i]);
9 }
10
11 // Modifying a grade
12 grades[1] = 93.0;
13 System.out.println("
14Updated Grade for Bob: " + grades[1]);
15 }
16}

Output

Output
Student Grades:
Alice: 85.5
Bob: 92.0
Charlie: 78.5

Updated Grade for Bob: 93.0

Summary

ConceptDescription
DeclarationDeclaring an array using dataType[] arrayName;.
InitializationInitializing an array with values using {value1, value2, ...}.
Accessing ElementsAccessing elements using their index arrayName[index].
Modifying ElementsModifying elements by assigning a new value to them.
Array LengthGetting the number of elements in an array using arrayName.length.
LoopingIterating over arrays using loops like for or enhanced for loop.
MultidimensionalArrays of arrays, allowing tabular data storage.

What's Next?

Now that you have a solid understanding of arrays in Java, the next step is to learn about methods. Methods are blocks of code that perform specific tasks and can be reused throughout your program. They allow for better organization and modularity of your code.

In the next tutorial, we will explore how to define, call, and use methods in Java. Stay tuned!


PreviousJava Break/ContinueNext Java Methods

Recommended Gear

Java Break/ContinueJava Methods