Skip to main content

Simple Array Example in Java

2 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Simple array is a classic Java console program that demonstrates the concept with complete source code and sample output. Arrays store fixed-size sequences with fast index access — a foundation before collections.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index by a mathematical formula. The simplest type of data structure is a linear array, also called one-dimensional array.

Syntax

Data_type[] Variable_name = new Data_type[Length];

Simple array Example Program

class SimpleArray{  
	public static void main(String args[]){
		int[] a=new int[5];
		a[0]=5;  
		a[1]=10;  
		a[2]=15;  
		a[3]=20;  
		a[4]=25;   
		for(int i=0;i < a.length;i++){
			System.out.println(a[i]);  
		}
	}
}

Sample Output

5
10
15
20
25

When to use

Use this simple array example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. int[] a=new int[5]; updates a variable used in the calculation or output.

  3. a[0]=5; updates a variable used in the calculation or output.

  4. a[1]=10; updates a variable used in the calculation or output.

  5. a[2]=15; updates a variable used in the calculation or output.

  6. a[3]=20; updates a variable used in the calculation or output.

  7. a[4]=25; updates a variable used in the calculation or output.

  8. A loop repeats the block until its condition becomes false.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Simple array program demonstrate?
It shows how to implement simple array in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials